#Code for "Do Self-Identified Tactical Voters Actually Vote Tactically? Evidence from the 2010 British General Election"
# by Sundeep Iyer
#Pre-campaign metrics
library(foreign)
library(car)
data <- read.dta("ygprecampostjune23d.dta") 
nrow(data)


#OK, now let's get to work on creating our matrix for all voters
prefparty <- matrix(data = NA, nrow = nrow(data), ncol = 13)
colnames(prefparty) <- c("metric1", "metric2", "metric3", "metric4", "metric5", "metric6", "metric7", "metric8", "metric9","metric10","metric11", "metric12", "metric13")


#metric 1: first we have to clean somethings up
dataother <- data[data$aaq4 == "other",]
#change a couple of these -> 9 (1673) to bnp, 55 (8721) to ukip
data$aaq4[1673] <- "british national party (bnp)"
data$aaq4[8721] <- "united kingdom independence party (ukip)"


data$aaq4condensed <- recode(data$aaq4,"'conservatives' = 1; 'liberal democrats' = 3; 'labour' = 2; 'none - no party' = 9; 'british national party (bnp)' = 4; 'united kingdom independence party (ukip)' = 5; 'green party' = 8; 'scottish national party (snp)' = 6; 'other' = 9; 'plaid cymru' = 7; 'there are no important problems' = 9; else = 9")
aaq4condensed <- as.numeric(data$aaq4condensed)
prefparty$metric1 <- aaq4condensed

#metric 2 creation: first clean things up
dataother <- data[data$aaq28 == "other",]
#change a couple of these -> 155 (13497) to BNP, 130 (11250) to UKIP, 43 (4248) to UKIP, 8 (473) to ukip
data$aaq28[13497] <- "british national party (bnp)"
data$aaq28[11250] <- "united kingdom independence party (ukip)"
data$aaq28[4248] <- "united kingdom independence party (ukip)"
data$aaq28[473] <- "united kingdom independence party (ukip)"

data$aaq28condensed <- recode(data$aaq28,"'conservative' = 1; 'liberal democrat' = 3; 'labour' = 2; 'british national party (bnp)' = 4; 'united kingdom independence party (ukip)' = 5; 'green party' = 8; 'scottish national party (snp)' = 6; 'plaid cymru' = 7; else = 9")

data$aaq30condensed <- recode(data$aaq30,"'conservative' = 1; 'liberal democrat' = 3; 'labour' = 2; 'british national party (bnp)' = 4; 'united kingdom independence party (ukip)' = 5; 'green party' = 8; 'scottish national party (snp)' = 6; 'plaid cymru' = 7; else = 9")

#let's deal with the "closer to" question - q30
for(i in 1:nrow(data)){
	if(data$aaq28condensed[i] %in% 9){
		prefparty$metric2[i] <- data$aaq30condensed[i]
		} else{prefparty$metric2[i] <- data$aaq28condensed[i]
			}}

#now let's create metric 3
data$aaq52condensed <- recode(data$aaq52, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq53condensed <- recode(data$aaq53, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq54condensed <- recode(data$aaq54, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq55condensed <- recode(data$aaq55, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq56condensed <- recode(data$aaq56, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

metric3cmat <- cbind(data$aaq52condensed,data$aaq53condensed,data$aaq54condensed,data$aaq55condensed,data$aaq56condensed)
metric3cmat <- as.matrix(metric3cmat)
colnames(metric3cmat) <- c("a", "b", "c", "d", "e")

#let's first decide who's going to be included by creating a matrix indicating who expressed a preference and who didn't on each question
metric3nonemat <- matrix(data = NA, nrow = nrow(data), ncol = 6)

for(i in 1:nrow(data)){
	if(metric3cmat[i,1] %in% 1){
		metric3nonemat[i,1] <- 1
		} else{metric3nonemat[i,1] <- 0}}
		
for(i in 1:nrow(data)){
	if(metric3cmat[i,2] %in% 1){
		metric3nonemat[i,2] <- 1
		} else{metric3nonemat[i,2] <- 0}}

for(i in 1:nrow(data)){
	if(metric3cmat[i,3] %in% 1){
		metric3nonemat[i,3] <- 1
		} else{metric3nonemat[i,3] <- 0}}
		
for(i in 1:nrow(data)){
	if(metric3cmat[i,4] %in% 1){
		metric3nonemat[i,4] <- 1
		} else{metric3nonemat[i,4] <- 0}}
		
for(i in 1:nrow(data)){
	if(metric3cmat[i,5] %in% 1){
		metric3nonemat[i,5] <- 1
		} else{metric3nonemat[i,5] <- 0}}

colnames(metric3nonemat) <- c("a", "b","c","d","e","f")
metric3nonemat <- as.data.frame(metric3nonemat)

for(i in 1:nrow(data)){
	metric3nonemat[i,6] <- sum(metric3nonemat[i,1],metric3nonemat[i,2],metric3nonemat[i,3],metric3nonemat[i,4],metric3nonemat[i,5])}

#now let's get to work constructing the actual metric


for(i in 1:nrow(data)){
	if(metric3nonemat[i,6] %in% c(0,1,2,3))
		{if(metric3cmat[i,1] > metric3cmat[i,2] & metric3cmat[i,1] > metric3cmat[i,3] & metric3cmat[i,1] > metric3cmat[i,4] & metric3cmat[i,1] > metric3cmat[i,5])
			{prefparty$metric3[i] <- 2}
			else{if(metric3cmat[i,2] > metric3cmat[i,1] & metric3cmat[i,2] > metric3cmat[i,3] & metric3cmat[i,2] > metric3cmat[i,4] & metric3cmat[i,2] > metric3cmat[i,5])
				{prefparty$metric3[i] <- 1}
				else{if(metric3cmat[i,3] > metric3cmat[i,1] & metric3cmat[i,3] > metric3cmat[i,2] & metric3cmat[i,3] > metric3cmat[i,4] & metric3cmat[i,3] > metric3cmat[i,5])
					{prefparty$metric3[i] <- 3}
					else{if(metric3cmat[i,4] > metric3cmat[i,1] & metric3cmat[i,4] > metric3cmat[i,2] & metric3cmat[i,4] > metric3cmat[i,3] & metric3cmat[i,4] > metric3cmat[i,5])
						{prefparty$metric3[i] <- 6}
						else{if(metric3cmat[i,5] > metric3cmat[i,1] & metric3cmat[i,5] > metric3cmat[i,2] & metric3cmat[i,5] > metric3cmat[i,3] & metric3cmat[i,5] > metric3cmat[i,4])
							{prefparty$metric3[i] <- 7}
							else{prefparty$metric3[i] <- 9}}
						}
					}
				}
		} 
		else{if(metric3nonemat[i,6] %in% c(4))
			{if(metric3cmat[i,1] >= 10)
				{prefparty$metric3[i] <- 2}
				else{if(metric3cmat[i,2] >= 10)
					{prefparty$metric3[i] <- 1}
					else{if(metric3cmat[i,3] >= 10)
						{prefparty$metric3[i] <- 3}
						else{if(metric3cmat[i,4] >= 10)
							{prefparty$metric3[i] <- 6}
							else{if(metric3cmat[i,5] >= 10)
								{prefparty$metric3[i] <- 7}
								else{prefparty$metric3[i] <- 9}
								}
							}
						}
					}
				}
			else{prefparty$metric3[i] <- 9}}
	}

#metric 4 creation
data$aaq63condensed <- recode(data$aaq63, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq64condensed <- recode(data$aaq64, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq65condensed <- recode(data$aaq65, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq66condensed <- recode(data$aaq66, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq67condensed <- recode(data$aaq67, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq68condensed <- recode(data$aaq68, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq69condensed <- recode(data$aaq69, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq70condensed <- recode(data$aaq70, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")


metric4matrix <- cbind(data$aaq63condensed,data$aaq64condensed,data$aaq65condensed,data$aaq66condensed,data$aaq67condensed,data$aaq68condensed,data$aaq69condensed,data$aaq70condensed)
metric4matrix <- as.matrix(metric4matrix)


#let's now decide who's going to be included by creating a matrix indicting who expressed a pref and who didn't for each question
metric4nonemat <- matrix(data = NA, nrow = nrow(data), ncol = 9)

for(i in 1:nrow(data)){
	if(metric4matrix[i,1] %in% 1){
		metric4nonemat[i,1] <- 1
		} else {metric4nonemat[i,1] <- 0}}
		
for(i in 1:nrow(data)){
	if(metric4matrix[i,2] %in% 1){
		metric4nonemat[i,2] <- 1
		} else {metric4nonemat[i,2] <- 0}}

for(i in 1:nrow(data)){
	if(metric4matrix[i,3] %in% 1){
		metric4nonemat[i,3] <- 1
		} else {metric4nonemat[i,3] <- 0}}

for(i in 1:nrow(data)){
	if(metric4matrix[i,4] %in% 1){
		metric4nonemat[i,4] <- 1
		} else {metric4nonemat[i,4] <- 0}}

for(i in 1:nrow(data)){
	if(metric4matrix[i,5] %in% 1){
		metric4nonemat[i,5] <- 1
		} else {metric4nonemat[i,5] <- 0}}

for(i in 1:nrow(data)){
	if(metric4matrix[i,6] %in% 1){
		metric4nonemat[i,6] <- 1
		} else {metric4nonemat[i,6] <- 0}}

for(i in 1:nrow(data)){
	if(metric4matrix[i,7] %in% 1){
		metric4nonemat[i,7] <- 1
		} else {metric4nonemat[i,7] <- 0}}

for(i in 1:nrow(data)){
	if(metric4matrix[i,8] %in% 1){
		metric4nonemat[i,8] <- 1
		} else {metric4nonemat[i,8] <- 0}}
		
for(i in 1:nrow(data)){
	metric4nonemat[i,9] <- sum(metric4nonemat[i,1], metric4nonemat[i,2],metric4nonemat[i,3],metric4nonemat[i,4],metric4nonemat[i,5],metric4nonemat[i,6],metric4nonemat[i,7],metric4nonemat[i,8])}
	
#now it's time to construct the actual metric!
for(i in 1:nrow(data))
	{if(metric4nonemat[i,9] %in% c(0,1,2,3,4,5,6))
		{if(metric4matrix[i,1] > metric4matrix[i,2] & metric4matrix[i,1] > metric4matrix[i,3] & metric4matrix[i,1] > metric4matrix[i,4] & metric4matrix[i,1] > metric4matrix[i,5] & metric4matrix[i,1] > metric4matrix[i,6] & metric4matrix[i,1] > metric4matrix[i,7] & metric4matrix[i,1] > metric4matrix[i,8])
			{prefparty$metric4[i] <- 2}
			else{if(metric4matrix[i,2] > metric4matrix[i,1] & metric4matrix[i,2] > metric4matrix[i,3] & metric4matrix[i,2] > metric4matrix[i,4] & metric4matrix[i,2] > metric4matrix[i,5] & metric4matrix[i,2] > metric4matrix[i,6] & metric4matrix[i,2] > metric4matrix[i,7] & metric4matrix[i,2] > metric4matrix[i,8])
				{prefparty$metric4[i] <- 1}
				else{if(metric4matrix[i,3] > metric4matrix[i,1] & metric4matrix[i,3] > metric4matrix[i,2] & metric4matrix[i,3] > metric4matrix[i,4] & metric4matrix[i,3] > metric4matrix[i,5] & metric4matrix[i,3] > metric4matrix[i,6] & metric4matrix[i,3] > metric4matrix[i,7] & metric4matrix[i,3] > metric4matrix[i,8])
					{prefparty$metric4[i] <- 3}
					else{if(metric4matrix[i,4] > metric4matrix[i,1] & metric4matrix[i,4] > metric4matrix[i,2] & metric4matrix[i,4] > metric4matrix[i,3] & metric4matrix[i,4] > metric4matrix[i,5] & metric4matrix[i,4] > metric4matrix[i,6] & metric4matrix[i,4] > metric4matrix[i,7] & metric4matrix[i,4] > metric4matrix[i,8])
						{prefparty$metric4[i] <- 8}
						else{if(metric4matrix[i,5] > metric4matrix[i,1] & metric4matrix[i,5] > metric4matrix[i,2] & metric4matrix[i,5] > metric4matrix[i,3] & metric4matrix[i,5] > metric4matrix[i,4] & metric4matrix[i,5] > metric4matrix[i,6] & metric4matrix[i,5] > metric4matrix[i,7] & metric4matrix[i,5] > metric4matrix[i,8])
							{prefparty$metric4[i] <- 5}
							else{if(metric4matrix[i,6] > metric4matrix[i,1] & metric4matrix[i,6] > metric4matrix[i,2] & metric4matrix[i,6] > metric4matrix[i,3] & metric4matrix[i,6] > metric4matrix[i,4] & metric4matrix[i,6] > metric4matrix[i,5] & metric4matrix[i,6] > metric4matrix[i,7] & metric4matrix[i,6] > metric4matrix[i,8])
								{prefparty$metric4[i] <- 4}
								else{if(metric4matrix[i,7] > metric4matrix[i,1] & metric4matrix[i,7] > metric4matrix[i,2] & metric4matrix[i,7] > metric4matrix[i,3] & metric4matrix[i,7] > metric4matrix[i,4] & metric4matrix[i,7] > metric4matrix[i,5] & metric4matrix[i,7] > metric4matrix[i,6] & metric4matrix[i,7] > metric4matrix[i,8])
									{prefparty$metric4[i] <- 6}
									else{if(metric4matrix[i,8] > metric4matrix[i,1] & metric4matrix[i,8] > metric4matrix[i,2] & metric4matrix[i,8] > metric4matrix[i,3] & metric4matrix[i,8] > metric4matrix[i,4] & metric4matrix[i,8] > metric4matrix[i,5] & metric4matrix[i,8] > metric4matrix[i,6] & metric4matrix[i,8] > metric4matrix[i,7])
										{prefparty$metric4[i] <- 7}
										else{prefparty$metric4[i] <- 9}
										}
									}
								}
							}
						}
					}
				}
			}
	else{if(metric4nonemat[i,9] %in% c(7))
		{if(metric4matrix[i,1] >= 10)
			{prefparty$metric4[i] <- 2}
			else{if(metric4matrix[i,2] >= 10)
				{prefparty$metric4[i] <- 1}
				else{if(metric4matrix[i,3] >= 10)
					{prefparty$metric4[i] <- 3}
					else{if(metric4matrix[i,4] >= 10)
						{prefparty$metric4[i] <- 8}
						else{if(metric4matrix[i,5] >= 10)
							{prefparty$metric4[i] <- 5}
							else{if(metric4matrix[i,6] >= 10)
								{prefparty$metric4[i] <- 4}
								else{if(metric4matrix[i,7] >= 10)
									{prefparty$metric4[i] <- 6}
									else{if(metric4matrix[i,8] >= 10)
										{prefparty$metric4[i] <- 7}
										else{prefparty$metric4[i] <- 9}
										}
									}
								}
							}
						}
					}
				}
			}
		else{prefparty$metric4[i] <- 9}}
		}

#now let's do metric 5!
data$aaq81condensed <- recode(data$aaq81, "'0-very incompetent' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very competent' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq82condensed <- recode(data$aaq82, "'0' = 2; '1' = 3; '2' = 4; '3' = 5; '4' = 6; '5' = 7; '6' = 8; '7' = 9; '8' = 10; '9' = 11; '10' = 12; 'dk' = 1; '<NA>' = 1; 'NA' = 1; else = 1")

data$aaq83condensed <- recode(data$aaq83, "'0-very incompetent' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very competent' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq84condensed <- recode(data$aaq84, "'0-very incompetent' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very competent' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

data$aaq85condensed <- recode(data$aaq85, "'0-very incompetent' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very competent' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

metric5matrix <- cbind(data$aaq81condensed,data$aaq82condensed,data$aaq83condensed,data$aaq84condensed,data$aaq85condensed)
metric5matrix <- as.matrix(metric5matrix)

#next, let's decide who's going to be included by creating a matrix corresponding to who expressed a pref and who didn't
metric5nonemat <- matrix(data = NA, nrow = nrow(data), ncol = 6)

for(i in 1:nrow(data)){
	if(metric5matrix[i,1] %in% 1){
		metric5nonemat[i,1] <- 1}
		else {metric5nonemat[i,1] <- 0}}

for(i in 1:nrow(data)){
	if(metric5matrix[i,2] %in% 1){
		metric5nonemat[i,2] <- 1}
		else {metric5nonemat[i,2] <- 0}}

for(i in 1:nrow(data)){
	if(metric5matrix[i,3] %in% 1){
		metric5nonemat[i,3] <- 1}
		else {metric5nonemat[i,3] <- 0}}

for(i in 1:nrow(data)){
	if(metric5matrix[i,4] %in% 1){
		metric5nonemat[i,4] <- 1}
		else {metric5nonemat[i,4] <- 0}}
		
for(i in 1:nrow(data)){
	if(metric5matrix[i,5] %in% 1){
		metric5nonemat[i,5] <- 1}
		else {metric5nonemat[i,5] <- 0}}
		
for(i in 1:nrow(data)){
	metric5nonemat[i,6] <- sum(metric5nonemat[i,1],metric5nonemat[i,2],metric5nonemat[i,3],metric5nonemat[i,4],metric5nonemat[i,5])}


#now let's create the metric
for(i in 1:nrow(data)){
	if(metric5nonemat[i,6] %in% c(0,1,2,3))
		{if(metric5matrix[i,1] > metric5matrix[i,2] & metric5matrix[i,1] > metric5matrix[i,3] & metric5matrix[i,1] > metric5matrix[i,4] & metric5matrix[i,1] > metric5matrix[i,5])
			{prefparty$metric5[i] <- 2}
			else{if(metric5matrix[i,2] > metric5matrix[i,1] & metric5matrix[i,2] > metric5matrix[i,3] & metric5matrix[i,2] > metric5matrix[i,4] & metric5matrix[i,2] > metric5matrix[i,5])
				{prefparty$metric5[i] <- 1}
				else{if(metric5matrix[i,3] > metric5matrix[i,1] & metric5matrix[i,3] > metric5matrix[i,2] & metric5matrix[i,3] > metric5matrix[i,4] & metric5matrix[i,3] > metric5matrix[i,5])
					{prefparty$metric5[i] <- 3}
					else{if(metric5matrix[i,4] > metric5matrix[i,1] & metric5matrix[i,4] > metric5matrix[i,2] & metric5matrix[i,4] > metric5matrix[i,3] & metric5matrix[i,4] > metric5matrix[i,5])
						{prefparty$metric5[i] <- 6}
						else{if(metric5matrix[i,5] > metric5matrix[i,1] & metric5matrix[i,5] > metric5matrix[i,2] & metric5matrix[i,5] > metric5matrix[i,3] & metric5matrix[i,5] > metric5matrix[i,4])
							{prefparty$metric5[i] <- 7}
							else{prefparty$metric5[i] <- 9}
							}
						}
					}
				}
			}
		else{if(metric5nonemat[i,6] %in% c(4))
			{if(metric5matrix[i,1] >= 10)
				{prefparty$metric5[i] <- 2}
				else{if(metric5matrix[i,2] >= 10)
					{prefparty$metric5[i] <- 1}
					else{if(metric5matrix[i,3] >= 10)
						{prefparty$metric5[i] <- 3}
						else{if(metric5matrix[i,4] >= 10)
							{prefparty$metric5[i] <- 6}
							else{if(metric5matrix[i,5] >= 10)
								{prefparty$metric5[i] <- 7}
								else{prefparty$metric5[i] <- 9}
								}
							}
						}
					}
				}
			else{prefparty$metric5[i] <- 9}}
			}

#now let's create metric 6
prefparty$metric6 <- as.numeric(recode(data$aaq90, "'conservatives' = 1; 'labour' = 2; else = 9"))
prefparty$metric6[prefparty$metric6 == 3] <- 9

#how about metric 7?
prefparty$metric7 <- as.numeric(recode(data$aaq95, "'david cameron' = 1; 'gordon brown' = 2; 'nick clegg' = 3; else = 9"))
prefparty$metric7[prefparty$metric7 == 4] <- 9


#now, let's create metric8
aaq104condensed <- recode(data$aaq104, "'0-cut taxes' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-increase taxes' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
data$aaq104condensed <- as.numeric(aaq104condensed) - 1
data$aaq104condensed[data$aaq104condensed == 11] <- 100


aaq105condensed <- recode(data$aaq105, "'0-cut taxes' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-increase taxes' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
data$aaq105condensed <- as.numeric(aaq105condensed) - 1
data$aaq105condensed[data$aaq105condensed == 11] <- 100


aaq106condensed <- recode(data$aaq106, "'0-cut taxes' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-increase taxes' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
data$aaq106condensed <- as.numeric(aaq106condensed) - 1
data$aaq106condensed[data$aaq106condensed == 11] <- 100


aaq107condensed <- recode(data$aaq107, "'0-cut taxes' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-increase taxes' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
data$aaq107condensed <- as.numeric(aaq107condensed) - 1
data$aaq107condensed[data$aaq107condensed == 11] <- 100


aaq108condensed <- recode(data$aaq108, "'0-cut taxes' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-increase taxes' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
data$aaq108condensed <- as.numeric(aaq108condensed) - 1
data$aaq108condensed[data$aaq108condensed == 11] <- 100


aaq109condensed <- recode(data$aaq109, "'0-cut taxes' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-increase taxes' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
data$aaq109condensed <- as.numeric(aaq109condensed) - 1
data$aaq109condensed[data$aaq109condensed == 11] <- 100


#now, we've created our preliminary measure. let's proceed now by creating metrics for the differences between the voter and each of the parties
partydiff8 <- matrix(data = NA, nrow = nrow(data), ncol = 5)

for(i in 1:nrow(data)){
	if(data$aaq104condensed[i] %in% 100)
	{partydiff8[i,1] <- 100}
	else{if(data$aaq105condensed[i] %in% 100)
		{partydiff8[i,1] <- 100}
		else{partydiff8[i,1] <- abs(data$aaq105condensed[i] - data$aaq104condensed[i])}}
		}

for(i in 1:nrow(data)){
	if(data$aaq104condensed[i] %in% 100)
	{partydiff8[i,2] <- 100}
	else{if(data$aaq106condensed[i] %in% 100)
		{partydiff8[i,2] <- 100}
		else{partydiff8[i,2] <- abs(data$aaq106condensed[i] - data$aaq104condensed[i])}}
		}
		
for(i in 1:nrow(data)){
	if(data$aaq104condensed[i] %in% 100)
	{partydiff8[i,3] <- 100}
	else{if(data$aaq107condensed[i] %in% 100)
		{partydiff8[i,3] <- 100}
		else{partydiff8[i,3] <- abs(data$aaq107condensed[i] - data$aaq104condensed[i])}}
		}
		
for(i in 1:nrow(data)){
	if(data$aaq104condensed[i] %in% 100)
	{partydiff8[i,4] <- 100}
	else{if(data$aaq108condensed[i] %in% 100)
		{partydiff8[i,4] <- 100}
		else{partydiff8[i,4] <- abs(data$aaq108condensed[i] - data$aaq104condensed[i])}}
		}

for(i in 1:nrow(data)){
	if(data$aaq104condensed[i] %in% 100)
	{partydiff8[i,5] <- 100}
	else{if(data$aaq109condensed[i] %in% 100)
		{partydiff8[i,5] <- 100}
		else{partydiff8[i,5] <- abs(data$aaq109condensed[i] - data$aaq104condensed[i])}}
		}

#now that we've created each of our individual party stuff
metric8nonemat <- matrix(data = NA, nrow = nrow(data), ncol = 6)

for(i in 1:nrow(data)){
	if(partydiff8[i,1] %in% 100)
	{metric8nonemat[i,1] <- 1}
	else{metric8nonemat[i,1] <- 0}}

for(i in 1:nrow(data)){
	if(partydiff8[i,2] %in% 100)
	{metric8nonemat[i,2] <- 1}
	else{metric8nonemat[i,2] <- 0}}

for(i in 1:nrow(data)){
	if(partydiff8[i,3] %in% 100)
	{metric8nonemat[i,3] <- 1}
	else{metric8nonemat[i,3] <- 0}}
	
for(i in 1:nrow(data)){
	if(partydiff8[i,4] %in% 100)
	{metric8nonemat[i,4] <- 1}
	else{metric8nonemat[i,4] <- 0}}
	
for(i in 1:nrow(data)){
	if(partydiff8[i,5] %in% 100)
	{metric8nonemat[i,5] <- 1}
	else{metric8nonemat[i,5] <- 0}}
	
for(i in 1:nrow(data)){
	metric8nonemat[i,6] <- sum(metric8nonemat[i,1],metric8nonemat[i,2],metric8nonemat[i,3],metric8nonemat[i,4],metric8nonemat[i,5])}


#now, time to get to work constructing our metric here
for(i in 1:nrow(data)){
	if(metric8nonemat[i,6] %in% c(0,1,2,3))
		{if(partydiff8[i,1] < partydiff8[i,2] & partydiff8[i,1] < partydiff8[i,3] & partydiff8[i,1] < partydiff8[i,4] & partydiff8[i,1] < partydiff8[i,5])
			{prefparty$metric8[i] <- 2}
			else{if(partydiff8[i,2] < partydiff8[i,1] & partydiff8[i,2] < partydiff8[i,3] & partydiff8[i,2] < partydiff8[i,4] & partydiff8[i,2] < partydiff8[i,5])
				{prefparty$metric8[i] <- 1}
				else{if(partydiff8[i,3] < partydiff8[i,1] & partydiff8[i,3] < partydiff8[i,2] & partydiff8[i,3] < partydiff8[i,4] & partydiff8[i,3] < partydiff8[i,5])
					{prefparty$metric8[i] <- 3}
					else{if(partydiff8[i,4] < partydiff8[i,1] & partydiff8[i,4] < partydiff8[i,2] & partydiff8[i,4] < partydiff8[i,3] & partydiff8[i,4] < partydiff8[i,5])
						{prefparty$metric8[i] <- 6}
						else{if(partydiff8[i,5] < partydiff8[i,1] & partydiff8[i,5] < partydiff8[i,2] & partydiff8[i,5] < partydiff8[i,3] & partydiff8[i,5] < partydiff8[i,4])
							{prefparty$metric8[i] <- 7}
							else{prefparty$metric8[i] <- 9}
							}
						}
					}
				}
		}
		else{if(metric8nonemat[i,6] %in% 4)
			{if(partydiff8[i,1] < 2)
				{prefparty$metric8[i] <- 2}
				else{if(partydiff8[i,2] < 2)
					{prefparty$metric8[i] <- 1}
					else{if(partydiff8[i,3] < 2)
						{prefparty$metric8[i] <- 3}
						else{if(partydiff8[i,4] < 2)
							{prefparty$metric8[i] <- 6}
							else{if(partydiff8[i,5] < 2)
								{prefparty$metric8[i] <- 7}
								else{prefparty$metric8[i] <- 9}
								}
							}
						}
					}
				}
			else{prefparty$metric8[i] <- 9}
			}
		}

#now let's create metric 9! almost there!
data$aaq189condensed <- recode(as.numeric(data$aaq189),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

data$aaq190condensed <- recode(as.numeric(data$aaq190),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

data$aaq191condensed <- recode(as.numeric(data$aaq191),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

metric9matrix <- cbind(data$aaq189condensed,data$aaq190condensed,data$aaq191condensed)
metric9matrix <- as.matrix(metric9matrix)


#now, let's create a preference matrix
metric9nonemat <- matrix(data = NA, nrow = nrow(data), ncol = 4)

for(i in 1:nrow(data)){
	if(metric9matrix[i,1] %in% 0)
	{metric9nonemat[i,1] <- 1}
	else{metric9nonemat[i,1] <- 0}}

for(i in 1:nrow(data)){
	if(metric9matrix[i,2] %in% 0)
	{metric9nonemat[i,2] <- 1}
	else{metric9nonemat[i,2] <- 0}}

for(i in 1:nrow(data)){
	if(metric9matrix[i,3] %in% 0)
	{metric9nonemat[i,3] <- 1}
	else{metric9nonemat[i,3] <- 0}}
	
for(i in 1:nrow(data)){
	metric9nonemat[i,4] <- sum(metric9nonemat[i,1],metric9nonemat[i,2],metric9nonemat[i,3])}
	

#finally, let's construct our metric
for(i in 1:nrow(data)){
	if(metric9nonemat[i,4] %in% c(0,1))
		{if(metric9matrix[i,1] > metric9matrix[i,2] & metric9matrix[i,1] > metric9matrix[i,3])
			{prefparty$metric9[i] <- 2}
			else{if(metric9matrix[i,2] > metric9matrix[i,1] & metric9matrix[i,2] > metric9matrix[i,3])
				{prefparty$metric9[i] <- 1}
				else{if(metric9matrix[i,3] > metric9matrix[i,1] & metric9matrix[i,3] > metric9matrix[i,2])
					{prefparty$metric9[i] <- 3}
					else{prefparty$metric9[i] <- 9}
					}
				}
			}
	else{if(metric9nonemat[i,4] %in% 2)
		{if(metric9matrix[i,1] >= 9)
			{prefparty$metric9[i] <- 2}
			else{if(metric9matrix[i,2] >= 9)
				{prefparty$metric9[i] <- 1}
				else{if(metric9matrix[i,3] >= 9)
				{prefparty$metric9[i] <- 3}
				else{prefparty$metric9[i] <- 9}
				}
			}
		}
		else{prefparty$metric9[i] <- 9}
		}
	}

#metric 10 creation!
data$aaq192condensed <- recode(as.numeric(data$aaq192),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

data$aaq193condensed <- recode(as.numeric(data$aaq193),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

data$aaq194condensed <- recode(as.numeric(data$aaq194),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

metric10matrix <- cbind(data$aaq192condensed,data$aaq193condensed,data$aaq194condensed)
metric10matrix <- as.matrix(metric10matrix)



#now, let's create a preference matrix
metric10nonemat <- matrix(data = NA, nrow = nrow(data), ncol = 4)

for(i in 1:nrow(data)){
	if(metric10matrix[i,1] %in% 0)
	{metric10nonemat[i,1] <- 1}
	else{metric10nonemat[i,1] <- 0}}

for(i in 1:nrow(data)){
	if(metric10matrix[i,2] %in% 0)
	{metric10nonemat[i,2] <- 1}
	else{metric10nonemat[i,2] <- 0}}

for(i in 1:nrow(data)){
	if(metric10matrix[i,3] %in% 0)
	{metric10nonemat[i,3] <- 1}
	else{metric10nonemat[i,3] <- 0}}
	
for(i in 1:nrow(data)){
	metric10nonemat[i,4] <- sum(metric10nonemat[i,1],metric10nonemat[i,2],metric10nonemat[i,3])}
	

#finally, let's construct our metric
for(i in 1:nrow(data)){
	if(metric10nonemat[i,4] %in% c(0,1))
		{if(metric10matrix[i,1] > metric10matrix[i,2] & metric10matrix[i,1] > metric10matrix[i,3])
			{prefparty$metric10[i] <- 2}
			else{if(metric10matrix[i,2] > metric10matrix[i,1] & metric10matrix[i,2] > metric10matrix[i,3])
				{prefparty$metric10[i] <- 1}
				else{if(metric10matrix[i,3] > metric10matrix[i,1] & metric10matrix[i,3] > metric10matrix[i,2])
					{prefparty$metric10[i] <- 3}
					else{prefparty$metric10[i] <- 9}
					}
				}
			}
	else{if(metric10nonemat[i,4] %in% 2)
		{if(metric10matrix[i,1] >= 9)
			{prefparty$metric10[i] <- 2}
			else{if(metric10matrix[i,2] >= 9)
				{prefparty$metric10[i] <- 1}
				else{if(metric10matrix[i,3] >= 9)
				{prefparty$metric10[i] <- 3}
				else{prefparty$metric10[i] <- 9}
				}
			}
		}
		else{prefparty$metric10[i] <- 9}
		}
	}

#finally, let's do metric 11
data$aaq195condensed <- recode(as.numeric(data$aaq195),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

data$aaq196condensed <- recode(as.numeric(data$aaq196),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

data$aaq197condensed <- recode(as.numeric(data$aaq197),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

metric11matrix <- cbind(data$aaq195condensed,data$aaq196condensed,data$aaq197condensed)
metric11matrix <- as.matrix(metric11matrix)

metric11nonemat <- matrix(data = NA, nrow = nrow(data), ncol = 4)

for(i in 1:nrow(data)){
	if(metric11matrix[i,1] %in% 0)
	{metric11nonemat[i,1] <- 1}
	else{metric11nonemat[i,1] <- 0}}

for(i in 1:nrow(data)){
	if(metric11matrix[i,2] %in% 0)
	{metric11nonemat[i,2] <- 1}
	else{metric11nonemat[i,2] <- 0}}

for(i in 1:nrow(data)){
	if(metric11matrix[i,3] %in% 0)
	{metric11nonemat[i,3] <- 1}
	else{metric11nonemat[i,3] <- 0}}
	
for(i in 1:nrow(data)){
	metric11nonemat[i,4] <- sum(metric11nonemat[i,1],metric11nonemat[i,2],metric11nonemat[i,3])}

#finally, let's construct our metric
for(i in 1:nrow(data)){
	if(metric11nonemat[i,4] %in% c(0,1))
		{if(metric11matrix[i,1] > metric11matrix[i,2] & metric11matrix[i,1] > metric11matrix[i,3])
			{prefparty$metric11[i] <- 2}
			else{if(metric11matrix[i,2] > metric11matrix[i,1] & metric11matrix[i,2] > metric11matrix[i,3])
				{prefparty$metric11[i] <- 1}
				else{if(metric11matrix[i,3] > metric11matrix[i,1] & metric11matrix[i,3] > metric11matrix[i,2])
					{prefparty$metric11[i] <- 3}
					else{prefparty$metric11[i] <- 9}
					}
				}
			}
	else{if(metric11nonemat[i,4] %in% 2)
		{if(metric11matrix[i,1] >= 9)
			{prefparty$metric11[i] <- 2}
			else{if(metric11matrix[i,2] >= 9)
				{prefparty$metric11[i] <- 1}
				else{if(metric11matrix[i,3] >= 9)
				{prefparty$metric11[i] <- 3}
				else{prefparty$metric11[i] <- 9}
				}
			}
		}
		else{prefparty$metric11[i] <- 9}
		}
	}

preferredparty <- matrix(data = NA, nrow = nrow(data), ncol = 11)
preferredparty <- cbind(prefparty$metric1, prefparty$metric2,prefparty$metric3,prefparty$metric4,prefparty$metric5,prefparty$metric6,prefparty$metric7,prefparty$metric8,prefparty$metric9,prefparty$metric10,prefparty$metric11)
preferredparty <- as.matrix(preferredparty)





#Post-election metrics
pedata <- data[!is.na(data$ccps_res),]
pedata$ccq25condensed <- as.numeric(recode(pedata$ccq25, "'conservative' = 1; 'liberal democrat' = 3; 'labour' = 2; 'british national party (bnp)' = 4; 'united kingdom independence party (ukip)' = 5; 'green party' = 8; 'scottish national party (snp)' = 6; 'plaid cymru' = 7; else = 9"))
vote <- matrix(data = NA, nrow = nrow(pedata), ncol = 1)
for(i in 1:nrow(pedata)){
	if(pedata$ccq24[i] %in% "yes, voted")
	{vote[i,1] <- pedata$ccq25condensed[i]}
	else{vote[i,1] <- 10}}

votechoice <- vote[,1]
pevotedata <- pedata[votechoice != 10,]
postprefpty <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 10)

#let's create metric 1
pevotedata$ccq4condensed <- recode(pevotedata$ccq4,"'conservative' = 1; 'liberal democrat' = 3; 'labour' = 2; 'none / no party' = 9; 'british national party (bnp)' = 4; 'united kingdom independence party (ukip)' = 5; 'green party' = 8; 'scottish national party (snp)' = 6; 'other' = 9; 'plaid cymru' = 7; 'there are no important problems' = 9; else = 9")
ccq4condensed <- as.numeric(pevotedata$ccq4condensed)
postprefpty[,1] <- ccq4condensed

#let's create metric 2
pevotedata$ccq28condensed <- recode(pevotedata$ccq28,"'conservative' = 1; 'liberal democrat' = 3; 'labour' = 2; 'british national party (bnp)' = 4; 'united kingdom independence party (ukip)' = 5; 'green party' = 8; 'scottish national party (snp)' = 6; 'plaid cymru' = 7; else = 9")

pevotedata$ccq30condensed <- recode(pevotedata$ccq30,"'conservative' = 1; 'liberal democrat' = 3; 'labour' = 2; 'british national party (bnp)' = 4; 'united kingdom independence party (ukip)' = 5; 'green party' = 8; 'scottish national party (snp)' = 6; 'plaid cymru' = 7; else = 9")

for(i in 1:nrow(pevotedata)){
	if(pevotedata$ccq28condensed[i] %in% 9){
		postprefpty[i,2] <- pevotedata$ccq30condensed[i]
		} else{postprefpty[i,2] <- pevotedata$ccq28condensed[i]
			}}

#let's create metric 3
pevotedata$ccq52condensed <- recode(pevotedata$ccq52, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pevotedata$ccq53condensed <- recode(pevotedata$ccq53, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pevotedata$ccq54condensed <- recode(pevotedata$ccq54, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pevotedata$ccq55condensed <- recode(pevotedata$ccq55, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pevotedata$ccq56condensed <- recode(pevotedata$ccq56, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pmetric3mat <- cbind(pevotedata$ccq52condensed,pevotedata$ccq53condensed,pevotedata$ccq54condensed,pevotedata$ccq55condensed,pevotedata$ccq56condensed)
pmetric3mat <- as.matrix(pmetric3mat)

#let's first decide who's going to be included by creating a matrix indicating who expressed a preference and who didn't on each question
pmetric3nonemat <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 6)

for(i in 1:nrow(pevotedata)){
	if(pmetric3mat[i,1] %in% 1){
		pmetric3nonemat[i,1] <- 1
		} else{pmetric3nonemat[i,1] <- 0}}
		
for(i in 1:nrow(pevotedata)){
	if(pmetric3mat[i,2] %in% 1){
		pmetric3nonemat[i,2] <- 1
		} else{pmetric3nonemat[i,2] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric3mat[i,3] %in% 1){
		pmetric3nonemat[i,3] <- 1
		} else{pmetric3nonemat[i,3] <- 0}}
		
for(i in 1:nrow(pevotedata)){
	if(pmetric3mat[i,4] %in% 1){
		pmetric3nonemat[i,4] <- 1
		} else{pmetric3nonemat[i,4] <- 0}}
				
for(i in 1:nrow(pevotedata)){
	if(pmetric3mat[i,5] %in% 1){
		pmetric3nonemat[i,5] <- 1
		} else{pmetric3nonemat[i,5] <- 0}}
		
pmetric3nonemat <- as.data.frame(metric3nonemat)
for(i in 1:nrow(data)){
	pmetric3nonemat[i,6] <- sum(pmetric3nonemat[i,1],pmetric3nonemat[i,2],pmetric3nonemat[i,3],pmetric3nonemat[i,4],pmetric3nonemat[i,5])}


#now let's get to work constructing the actual metric


for(i in 1:nrow(pevotedata)){
	if(pmetric3nonemat[i,6] %in% c(0,1,2,3))
		{if(pmetric3mat[i,1] > pmetric3mat[i,2] & pmetric3mat[i,1] > pmetric3mat[i,3] & pmetric3mat[i,1] > pmetric3mat[i,4] & pmetric3mat[i,1] > pmetric3mat[i,5])
			{postprefpty[i,3] <- 2}
			else{if(pmetric3mat[i,2] > pmetric3mat[i,1] & pmetric3mat[i,2] > pmetric3mat[i,3] & pmetric3mat[i,2] > pmetric3mat[i,4] & pmetric3mat[i,2] > pmetric3mat[i,5])
				{postprefpty[i,3] <- 1}
				else{if(pmetric3mat[i,3] > pmetric3mat[i,1] & pmetric3mat[i,3] > pmetric3mat[i,2] & pmetric3mat[i,3] > pmetric3mat[i,4] & pmetric3mat[i,3] > pmetric3mat[i,5])
					{postprefpty[i,3] <- 3}
					else{if(pmetric3mat[i,4] > pmetric3mat[i,1] & pmetric3mat[i,4] > pmetric3mat[i,2] & pmetric3mat[i,4] > pmetric3mat[i,3] & pmetric3mat[i,4] > pmetric3mat[i,5])
						{postprefpty[i,3] <- 6}
						else{if(pmetric3mat[i,5] > pmetric3mat[i,1] & pmetric3mat[i,5] > pmetric3mat[i,2] & pmetric3mat[i,5] > pmetric3mat[i,3] & pmetric3mat[i,5] > pmetric3mat[i,4])
							{postprefpty[i,3] <- 7}
							else{postprefpty[i,3] <- 9}}
						}
					}
				}
		} 
		else{if(pmetric3nonemat[i,6] %in% c(4))
			{if(pmetric3mat[i,1] >= 10)
				{postprefpty[i,3] <- 2}
				else{if(pmetric3mat[i,2] >= 10)
					{postprefpty[i,3] <- 1}
					else{if(pmetric3mat[i,3] >= 10)
						{postprefpty[i,3] <- 3}
						else{if(pmetric3mat[i,4] >= 10)
							{postprefpty[i,3] <- 6}
							else{if(pmetric3mat[i,5] >= 10)
								{postprefpty[i,3] <- 7}
								else{postprefpty[i,3] <- 9}
								}
							}
						}
					}
				}
			else{postprefpty[i,3] <- 9}}
	}

#metric 4 creation:
pevotedata$ccq63condensed <- recode(pevotedata$ccq63, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pevotedata$ccq64condensed <- recode(pevotedata$ccq64, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pevotedata$ccq65condensed <- recode(pevotedata$ccq65, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pevotedata$ccq69condensed <- recode(pevotedata$ccq69, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pevotedata$ccq70condensed <- recode(pevotedata$ccq70, "'0-strongly dislike' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-strongly like' = 9; 'dk' = -1; '<NA>' = -1; else = -1")

pmetric4matrix <- cbind(pevotedata$ccq63condensed,pevotedata$ccq64condensed,pevotedata$ccq65condensed,pevotedata$ccq69condensed,pevotedata$ccq70condensed)
pmetric4mat <- as.matrix(pmetric4matrix)

pmetric4nonemat <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 6)
for(i in 1:nrow(pevotedata)){
	if(pmetric4mat[i,1] %in% 1){
		pmetric4nonemat[i,1] <- 1
		} else {pmetric4nonemat[i,1] <- 0}}
		
for(i in 1:nrow(pevotedata)){
	if(pmetric4mat[i,2] %in% 1){
		pmetric4nonemat[i,2] <- 1
		} else {pmetric4nonemat[i,2] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric4mat[i,3] %in% 1){
		pmetric4nonemat[i,3] <- 1
		} else {pmetric4nonemat[i,3] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric4mat[i,4] %in% 1){
		pmetric4nonemat[i,4] <- 1
		} else {pmetric4nonemat[i,4] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric4mat[i,5] %in% 1){
		pmetric4nonemat[i,5] <- 1
		} else {pmetric4nonemat[i,5] <- 0}}

for(i in 1:nrow(pevotedata)){
	pmetric4nonemat[i,6] <- sum(pmetric4nonemat[i,1], pmetric4nonemat[i,2],pmetric4nonemat[i,3],pmetric4nonemat[i,4],pmetric4nonemat[i,5])}
	

#now let's get to work constructing the actual metric


for(i in 1:nrow(pevotedata)){
	if(pmetric4nonemat[i,6] %in% c(0,1,2,3))
		{if(pmetric4mat[i,1] > pmetric4mat[i,2] & pmetric4mat[i,1] > pmetric4mat[i,3] & pmetric4mat[i,1] > pmetric4mat[i,4] & pmetric4mat[i,1] > pmetric4mat[i,5])
			{postprefpty[i,4] <- 2}
			else{if(pmetric4mat[i,2] > pmetric4mat[i,1] & pmetric4mat[i,2] > pmetric4mat[i,3] & pmetric4mat[i,2] > pmetric4mat[i,4] & pmetric4mat[i,2] > pmetric4mat[i,5])
				{postprefpty[i,4] <- 1}
				else{if(pmetric4mat[i,3] > pmetric4mat[i,1] & pmetric4mat[i,3] > pmetric4mat[i,2] & pmetric4mat[i,3] > pmetric4mat[i,4] & pmetric4mat[i,3] > pmetric4mat[i,5])
					{postprefpty[i,4] <- 3}
					else{if(pmetric4mat[i,4] > pmetric4mat[i,1] & pmetric4mat[i,4] > pmetric4mat[i,2] & pmetric4mat[i,4] > pmetric4mat[i,3] & pmetric4mat[i,4] > pmetric4mat[i,5])
						{postprefpty[i,4] <- 6}
						else{if(pmetric4mat[i,5] > pmetric4mat[i,1] & pmetric4mat[i,5] > pmetric4mat[i,2] & pmetric4mat[i,5] > pmetric4mat[i,3] & pmetric4mat[i,5] > pmetric4mat[i,4])
							{postprefpty[i,4] <- 7}
							else{postprefpty[i,4] <- 9}}
						}
					}
				}
		} 
		else{if(pmetric4nonemat[i,6] %in% c(4))
			{if(pmetric4mat[i,1] >= 10)
				{postprefpty[i,4] <- 2}
				else{if(pmetric4mat[i,2] >= 10)
					{postprefpty[i,4] <- 1}
					else{if(pmetric4mat[i,3] >= 10)
						{postprefpty[i,4] <- 3}
						else{if(pmetric4mat[i,4] >= 10)
							{postprefpty[i,4] <- 6}
							else{if(pmetric4mat[i,5] >= 10)
								{postprefpty[i,4] <- 7}
								else{postprefpty[i,4] <- 9}
								}
							}
						}
					}
				}
			else{postprefpty[i,4] <- 9}}
	}

#post-metric 5
postprefpty[,5] <- as.numeric(recode(pevotedata$ccq90, "'conservatives' = 1; 'labour' = 2; else = 9"))
postprefpty[postprefpty[,5] == 3,5] <- 9

#post-metric 6
pevotedata$ccq189condensed <- recode(as.numeric(pevotedata$ccq189),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pevotedata$ccq190condensed <- recode(as.numeric(pevotedata$ccq190),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pevotedata$ccq191condensed <- recode(as.numeric(pevotedata$ccq191),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pmetric6matrix <- cbind(pevotedata$ccq189condensed, pevotedata$ccq190condensed, pevotedata$ccq191condensed)
pmetric6matrix <- as.matrix(pmetric6matrix)

#now, let's create a preference matrix
pmetric6nonemat <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 4)

for(i in 1:nrow(pevotedata)){
	if(pmetric6matrix[i,1] %in% 0)
	{pmetric6nonemat[i,1] <- 1}
	else{pmetric6nonemat[i,1] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric6matrix[i,2] %in% 0)
	{pmetric6nonemat[i,2] <- 1}
	else{pmetric6nonemat[i,2] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric6matrix[i,3] %in% 0)
	{pmetric6nonemat[i,3] <- 1}
	else{pmetric6nonemat[i,3] <- 0}}
	
for(i in 1:nrow(pevotedata)){
	pmetric6nonemat[i,4] <- sum(pmetric6nonemat[i,1],pmetric6nonemat[i,2],pmetric6nonemat[i,3])}


#now let's construct our metric
for(i in 1:nrow(pevotedata)){
	if(pmetric6nonemat[i,4] %in% c(0,1))
		{if(pmetric6matrix[i,1] > pmetric6matrix[i,2] & pmetric6matrix[i,1] > pmetric6matrix[i,3])
			{postprefpty[i,6] <- 2}
			else{if(pmetric6matrix[i,2] > pmetric6matrix[i,1] & pmetric6matrix[i,2] > pmetric6matrix[i,3])
				{postprefpty[i,6] <- 1}
				else{if(pmetric6matrix[i,3] > pmetric6matrix[i,1] & pmetric6matrix[i,3] > pmetric6matrix[i,2])
					{postprefpty[i,6] <- 3}
					else{postprefpty[i,6] <- 9}
					}
				}
			}
	else{if(pmetric6nonemat[i,4] %in% 2)
		{if(pmetric6matrix[i,1] >= 9)
			{postprefpty[i,6] <- 2}
			else{if(pmetric6matrix[i,2] >= 9)
				{postprefpty[i,6] <- 1}
				else{if(pmetric6matrix[i,3] >= 9)
				{postprefpty[i,6] <- 3}
				else{postprefpty[i,6] <- 9}
				}
			}
		}
		else{postprefpty[i,6] <- 9}
		}
	}

#metric 7
pevotedata$ccq192condensed <- recode(as.numeric(pevotedata$ccq192),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pevotedata$ccq193condensed <- recode(as.numeric(pevotedata$ccq193),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pevotedata$ccq194condensed <- recode(as.numeric(pevotedata$ccq194),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pmetric7matrix <- cbind(pevotedata$ccq192condensed, pevotedata$ccq193condensed, pevotedata$ccq194condensed)
pmetric7matrix <- as.matrix(pmetric7matrix)

#now, let's create a preference matrix
pmetric7nonemat <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 4)


for(i in 1:nrow(pevotedata)){
	if(pmetric7matrix[i,1] %in% 0)
	{pmetric7nonemat[i,1] <- 1}
	else{pmetric7nonemat[i,1] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric7matrix[i,2] %in% 0)
	{pmetric7nonemat[i,2] <- 1}
	else{pmetric7nonemat[i,2] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric7matrix[i,3] %in% 0)
	{pmetric7nonemat[i,3] <- 1}
	else{pmetric7nonemat[i,3] <- 0}}
	
for(i in 1:nrow(pevotedata)){
	pmetric7nonemat[i,4] <- sum(pmetric7nonemat[i,1],pmetric7nonemat[i,2],pmetric7nonemat[i,3])}


#now let's construct our metric
for(i in 1:nrow(pevotedata)){
	if(pmetric7nonemat[i,4] %in% c(0,1))
		{if(pmetric7matrix[i,1] > pmetric7matrix[i,2] & pmetric7matrix[i,1] > pmetric7matrix[i,3])
			{postprefpty[i,7] <- 2}
			else{if(pmetric7matrix[i,2] > pmetric7matrix[i,1] & pmetric7matrix[i,2] > pmetric7matrix[i,3])
				{postprefpty[i,7] <- 1}
				else{if(pmetric7matrix[i,3] > pmetric7matrix[i,1] & pmetric7matrix[i,3] > pmetric7matrix[i,2])
					{postprefpty[i,7] <- 3}
					else{postprefpty[i,7] <- 9}
					}
				}
			}
	else{if(pmetric7nonemat[i,4] %in% 2)
		{if(pmetric7matrix[i,1] >= 9)
			{postprefpty[i,7] <- 2}
			else{if(pmetric7matrix[i,2] >= 9)
				{postprefpty[i,7] <- 1}
				else{if(pmetric7matrix[i,3] >= 9)
				{postprefpty[i,7] <- 3}
				else{postprefpty[i,7] <- 9}
				}
			}
		}
		else{postprefpty[i,7] <- 9}
		}
	}

#metric 8
pevotedata$ccq195condensed <- recode(as.numeric(pevotedata$ccq195),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pevotedata$ccq196condensed <- recode(as.numeric(pevotedata$ccq196),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pevotedata$ccq197condensed <- recode(as.numeric(pevotedata$ccq197),"'1'= 1;'2' = 2; '3' = 3; '4' = 4; '5' = 5; '6' = 6; '7' = 7; '8' = 8; '9' = 9; '10' = 10; '11' = 11; 'dk' = 0; '<NA>' = 0; else = 0")

pmetric8matrix <- cbind(pevotedata$ccq195condensed, pevotedata$ccq196condensed, pevotedata$ccq197condensed)
pmetric8matrix <- as.matrix(pmetric8matrix)

#now, let's create a preference matrix
pmetric8nonemat <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 4)


for(i in 1:nrow(pevotedata)){
	if(pmetric8matrix[i,1] %in% 0)
	{pmetric8nonemat[i,1] <- 1}
	else{pmetric8nonemat[i,1] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric8matrix[i,2] %in% 0)
	{pmetric8nonemat[i,2] <- 1}
	else{pmetric8nonemat[i,2] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(pmetric8matrix[i,3] %in% 0)
	{pmetric8nonemat[i,3] <- 1}
	else{pmetric8nonemat[i,3] <- 0}}
	
for(i in 1:nrow(pevotedata)){
	pmetric8nonemat[i,4] <- sum(pmetric8nonemat[i,1],pmetric8nonemat[i,2],pmetric8nonemat[i,3])}


#now let's construct our metric
for(i in 1:nrow(pevotedata)){
	if(pmetric8nonemat[i,4] %in% c(0,1))
		{if(pmetric8matrix[i,1] > pmetric8matrix[i,2] & pmetric8matrix[i,1] > pmetric8matrix[i,3])
			{postprefpty[i,8] <- 2}
			else{if(pmetric8matrix[i,2] > pmetric8matrix[i,1] & pmetric8matrix[i,2] > pmetric8matrix[i,3])
				{postprefpty[i,8] <- 1}
				else{if(pmetric8matrix[i,3] > pmetric8matrix[i,1] & pmetric8matrix[i,3] > pmetric8matrix[i,2])
					{postprefpty[i,8] <- 3}
					else{postprefpty[i,8] <- 9}
					}
				}
			}
	else{if(pmetric8nonemat[i,4] %in% 2)
		{if(pmetric8matrix[i,1] >= 9)
			{postprefpty[i,8] <- 2}
			else{if(pmetric8matrix[i,2] >= 9)
				{postprefpty[i,8] <- 1}
				else{if(pmetric8matrix[i,3] >= 9)
				{postprefpty[i,8] <- 3}
				else{postprefpty[i,8] <- 9}
				}
			}
		}
		else{postprefpty[i,8] <- 9}
		}
	}

#creating metric 9
postprefpty[,9] <- as.numeric(recode(pevotedata$ccq46, "'david cameron' = 1; 'gordon brown' = 2; 'nick clegg' = 3; else = 9"))
postprefpty[postprefpty[,9] == 4,9] <- 9

#creating metric 10
ccq110condensed <- recode(pevotedata$ccq110, "'0-reduce crime' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-rights accused' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
pevotedata$ccq110condensed <- as.numeric(ccq110condensed) - 1
pevotedata$ccq110condensed[pevotedata$ccq110condensed == 11] <- 100

ccq111condensed <- recode(pevotedata$ccq111, "'0-reduce crime' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-rights accused' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
pevotedata$ccq111condensed <- as.numeric(ccq111condensed) - 1
pevotedata$ccq111condensed[pevotedata$ccq111condensed == 11] <- 100

ccq112condensed <- recode(pevotedata$ccq112, "'0-reduce crime' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-rights accused' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
pevotedata$ccq112condensed <- as.numeric(ccq112condensed) - 1
pevotedata$ccq112condensed[pevotedata$ccq112condensed == 11] <- 100

ccq113condensed <- recode(pevotedata$ccq113, "'0-reduce crime' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-rights accused' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
pevotedata$ccq113condensed <- as.numeric(ccq113condensed) - 1
pevotedata$ccq113condensed[pevotedata$ccq113condensed == 11] <- 100

ccq114condensed <- recode(pevotedata$ccq114, "'0-reduce crime' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-rights accused' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
pevotedata$ccq114condensed <- as.numeric(ccq114condensed) - 1
pevotedata$ccq114condensed[pevotedata$ccq114condensed == 11] <- 100

ccq115condensed <- recode(pevotedata$ccq115, "'0-reduce crime' = -1; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-rights accused' = 9; 'dk' = 99; '<NA>' = 99; else = 99")
pevotedata$ccq115condensed <- as.numeric(ccq115condensed) - 1
pevotedata$ccq115condensed[pevotedata$ccq115condensed == 11] <- 100



#now, we've created our preliminary measure. let's proceed now by creating metrics for the differences between the voter and each of the parties
ppartydiff8 <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 5)

for(i in 1:nrow(pevotedata)){
	if(pevotedata$ccq110condensed[i] %in% 100)
	{ppartydiff8[i,1] <- 100}
	else{if(pevotedata$ccq111condensed[i] %in% 100)
		{ppartydiff8[i,1] <- 100}
		else{ppartydiff8[i,1] <- abs(pevotedata$ccq111condensed[i] - pevotedata$ccq110condensed[i])}}
		}

for(i in 1:nrow(pevotedata)){
	if(pevotedata$ccq110condensed[i] %in% 100)
	{ppartydiff8[i,2] <- 100}
	else{if(pevotedata$ccq112condensed[i] %in% 100)
		{ppartydiff8[i,2] <- 100}
		else{ppartydiff8[i,2] <- abs(pevotedata$ccq112condensed[i] - pevotedata$ccq110condensed[i])}}
		}

for(i in 1:nrow(pevotedata)){
	if(pevotedata$ccq110condensed[i] %in% 100)
	{ppartydiff8[i,3] <- 100}
	else{if(pevotedata$ccq113condensed[i] %in% 100)
		{ppartydiff8[i,3] <- 100}
		else{ppartydiff8[i,3] <- abs(pevotedata$ccq113condensed[i] - pevotedata$ccq110condensed[i])}}
		}
		
for(i in 1:nrow(pevotedata)){
	if(pevotedata$ccq110condensed[i] %in% 100)
	{ppartydiff8[i,4] <- 100}
	else{if(pevotedata$ccq114condensed[i] %in% 100)
		{ppartydiff8[i,4] <- 100}
		else{ppartydiff8[i,4] <- abs(pevotedata$ccq114condensed[i] - pevotedata$ccq110condensed[i])}}
		}

for(i in 1:nrow(pevotedata)){
	if(pevotedata$ccq110condensed[i] %in% 100)
	{ppartydiff8[i,5] <- 100}
	else{if(pevotedata$ccq115condensed[i] %in% 100)
		{ppartydiff8[i,5] <- 100}
		else{ppartydiff8[i,5] <- abs(pevotedata$ccq115condensed[i] - pevotedata$ccq110condensed[i])}}
		}

#now that we've created each of our individual party stuff
pmetric8nonemat <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 6)

for(i in 1:nrow(pevotedata)){
	if(ppartydiff8[i,1] %in% 100)
	{pmetric8nonemat[i,1] <- 1}
	else{pmetric8nonemat[i,1] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(ppartydiff8[i,2] %in% 100)
	{pmetric8nonemat[i,2] <- 1}
	else{pmetric8nonemat[i,2] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(ppartydiff8[i,3] %in% 100)
	{pmetric8nonemat[i,3] <- 1}
	else{pmetric8nonemat[i,3] <- 0}}

for(i in 1:nrow(pevotedata)){
	if(ppartydiff8[i,4] %in% 100)
	{pmetric8nonemat[i,4] <- 1}
	else{pmetric8nonemat[i,4] <- 0}}
	
for(i in 1:nrow(pevotedata)){
	if(ppartydiff8[i,5] %in% 100)
	{pmetric8nonemat[i,5] <- 1}
	else{pmetric8nonemat[i,5] <- 0}}

for(i in 1:nrow(pevotedata)){
	pmetric8nonemat[i,6] <- sum(pmetric8nonemat[i,1],pmetric8nonemat[i,2],pmetric8nonemat[i,3],pmetric8nonemat[i,4],pmetric8nonemat[i,5])}
	
	


#now, time to get to work constructing our metric here
for(i in 1:nrow(pevotedata)){
	if(pmetric8nonemat[i,6] %in% c(0,1,2,3))
		{if(ppartydiff8[i,1] < ppartydiff8[i,2] & ppartydiff8[i,1] < ppartydiff8[i,3] & ppartydiff8[i,1] < ppartydiff8[i,4] & ppartydiff8[i,1] < ppartydiff8[i,5])
			{postprefpty[i,10] <- 2}
			else{if(ppartydiff8[i,2] < ppartydiff8[i,1] & ppartydiff8[i,2] < ppartydiff8[i,3] & ppartydiff8[i,2] < ppartydiff8[i,4] & ppartydiff8[i,2] < ppartydiff8[i,5])
				{postprefpty[i,10] <- 1}
				else{if(ppartydiff8[i,3] < ppartydiff8[i,1] & ppartydiff8[i,3] < ppartydiff8[i,2] & ppartydiff8[i,3] < ppartydiff8[i,4] & ppartydiff8[i,3] < ppartydiff8[i,5])
					{postprefpty[i,10] <- 3}
					else{if(ppartydiff8[i,4] < ppartydiff8[i,1] & ppartydiff8[i,4] < ppartydiff8[i,2] & ppartydiff8[i,4] < ppartydiff8[i,3] & ppartydiff8[i,4] < ppartydiff8[i,5])
						{postprefpty[i,10] <- 6}
						else{if(ppartydiff8[i,5] < ppartydiff8[i,1] & ppartydiff8[i,5] < ppartydiff8[i,2] & ppartydiff8[i,5] < ppartydiff8[i,3] & ppartydiff8[i,5] < ppartydiff8[i,4])
							{postprefpty[i,10] <- 7}
							else{postprefpty[i,10] <- 9}
							}
						}
					}
				}
		}
		else{if(pmetric8nonemat[i,6] %in% 4)
			{if(ppartydiff8[i,1] < 2)
				{postprefpty[i,10] <- 2}
				else{if(ppartydiff8[i,2] < 2)
					{postprefpty[i,10] <- 1}
					else{if(ppartydiff8[i,3] < 2)
						{postprefpty[i,10] <- 3}
						else{if(ppartydiff8[i,4] < 2)
							{postprefpty[i,10] <- 6}
							else{if(ppartydiff8[i,5] < 2)
								{postprefpty[i,10] <- 7}
								else{postprefpty[i,10] <- 9}
								}
							}
						}
					}
				}
			else{postprefpty[i,10] <- 9}
			}
		}


#estimating party preference, EAP
preferredpartyrevised <- cbind(preferredparty[,2], preferredparty[,4], preferredparty[,1], preferredparty[,8], preferredparty[,3], preferredparty[,5], preferredparty[,9], preferredparty[,10], preferredparty[,11], preferredparty[,7])

preferredpartyrevised <- preferredpartyrevised[!is.na(data$ccps_res),]

preferredpartyrevised <- preferredpartyrevised[votechoice != 10,]

finalpartyprefrevised <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 2)

modefx <- function(x){as.numeric(names(sort(-table(x)))[1])}

modefx9 <- function(x){
	if(as.numeric(names(sort(-table(x)))[1]) == 9 & length(as.numeric(names(sort(-table(x))))) >= 2)
	{return(as.numeric(names(sort(-table(x)))[2]))}
	else{return(as.numeric(names(sort(-table(x)))[1]))}
	}



for(i in 1:nrow(preferredpartyrevised)){
	if(preferredpartyrevised[i,1] == preferredpartyrevised[i,2] & preferredpartyrevised[i,1] == preferredpartyrevised[i,3] & preferredpartyrevised[i,2] == preferredpartyrevised[i,3])
	{finalpartyprefrevised[i,1] <- preferredpartyrevised[i,2]}
	else{if(modefx(preferredpartyrevised[i,1:3]) == sort(preferredpartyrevised[i,1:3])[2] & sort(preferredpartyrevised[i,1:3])[2] != 9)
	{finalpartyprefrevised[i,1] <- modefx(preferredpartyrevised[i,1:3])}
	else{if(modefx(preferredpartyrevised[i,1:3]) == sort(preferredpartyrevised[i,1:3])[2] & sort(preferredpartyrevised[i,1:3])[2] == 9)
		{if(sort(preferredpartyrevised[i,1:3])[1] %in% c(1,2,3) & (modefx(preferredpartyrevised[i,4:10]) == sort(preferredpartyrevised[i,1:3])[1] | (preferredpartyrevised[i,4] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,5] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,6] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,7] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,8] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,9] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,10] %in% c(sort(preferredpartyrevised[i,1:3])[1],9))))
			{finalpartyprefrevised[i,1] <- sort(preferredpartyrevised[i,1:3])[1]}
			else{if(sort(preferredpartyrevised[i,1:3])[1] %in% c(6,7) & (modefx(preferredpartyrevised[i,4:6]) == sort(preferredpartyrevised[i,1:3])[1] | (preferredpartyrevised[i,4] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,5] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,6] %in% c(sort(preferredpartyrevised[i,1:3])[1],9))))
				{finalpartyprefrevised[i,1] <- sort(preferredpartyrevised[i,1:3])[1]}
					else{if(sort(preferredpartyrevised[i,1:3])[1] %in% c(4,5,8))
					{finalpartyprefrevised[i,1] <- sort(preferredpartyrevised[i,1:3])[1]}
					else{finalpartyprefrevised[i,1] <- 9}}}}
		else{if(preferredpartyrevised[i,1] != preferredpartyrevised[i,2] & preferredpartyrevised[i,1] != preferredpartyrevised[i,3] & preferredpartyrevised[i,2] != preferredpartyrevised[i,3])
			{if(preferredpartyrevised[i,1] %in% c(4,5,8) | preferredpartyrevised[i,2] %in% c(4,5,8) | preferredpartyrevised[i,3] %in% c(4,5,8))
				{finalpartyprefrevised[i,1] <- 9}
				else{if((preferredpartyrevised[i,1] %in% c(6,7) | preferredpartyrevised[i,2] %in% c(6,7) | preferredpartyrevised[i,3] %in% c(6,7)) & (modefx9(preferredpartyrevised[i,4:6]) == preferredpartyrevised[i,1] | modefx9(preferredpartyrevised[i,4:6]) == preferredpartyrevised[i,2] | modefx9(preferredpartyrevised[i,4:6]) == preferredpartyrevised[i,3]))
				{finalpartyprefrevised[i,1] <- modefx9(preferredpartyrevised[i,4:6])}
				else{if(modefx9(preferredpartyrevised[i,4:10]) == preferredpartyrevised[i,1] | modefx9(preferredpartyrevised[i,4:10]) == preferredpartyrevised[i,2] | modefx9(preferredpartyrevised[i,4:10]) == preferredpartyrevised[i,3])
					{finalpartyprefrevised[i,1] <- modefx9(preferredpartyrevised[i,4:10])}
					else{finalpartyprefrevised[i,1] <- 9}
					}
					}
				}
				else{finalpartyprefrevised[i,1] <- 9}
			}
			}
		}
	}

#let's graph the party preferences we get
hist(finalpartyprefrevised[,1], breaks = c(0,1,2,3,4,5,6,7,8,9), xlab = "Party Preference", ylab = "Percent of Voters", col = "red", main = "Party Preferences, Pre-Campaign Imputation", axes = FALSE, ylim = c(0,0.36*nrow(pevotedata)))

axis(1,c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5), labels = c("Con", "Lab", "LDem", "BNP", "UKIP", "SNP", "PC", "Green", "DNK"))

axis(2,c(0,0.06*nrow(pevotedata),0.12*nrow(pevotedata),0.18*nrow(pevotedata),0.24*nrow(pevotedata),0.30*nrow(pevotedata), 0.36*nrow(pevotedata)), labels = c("0%", "6%","12%","18%","24%","30%","36\%"))


#now let's graph the preferences from metric 1
hist(preferredpartyrevised[,1], breaks = c(0,1,2,3,4,5,6,7,8,9), xlab = "Party Preference", ylab = "Percent of Voters", col = "red", main = "Party Preferences, Metric 1", axes = FALSE, ylim = c(0,0.36*nrow(pevotedata)))

axis(1,c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5), labels = c("Con", "Lab", "LDem", "BNP", "UKIP", "SNP", "PC", "Green", "DNK"))

axis(2,c(0,0.06*nrow(pevotedata),0.12*nrow(pevotedata),0.18*nrow(pevotedata),0.24*nrow(pevotedata),0.30*nrow(pevotedata), 0.36*nrow(pevotedata)), labels = c("0%", "6%","12%","18%","24%","30%","36\%"))

#calculating the clarity scores
for(i in 1:nrow(preferredpartyrevised)){
	if(finalpartyprefrevised[i,1] == 9)
	{finalpartyprefrevised[i,2] <- 0}
	else{if(preferredpartyrevised[i,1] == preferredpartyrevised[i,2] & preferredpartyrevised[i,1] == preferredpartyrevised[i,3] & preferredpartyrevised[i,2] == preferredpartyrevised[i,3])
		{finalpartyprefrevised[i,2] <- 1}
		else{if(modefx(preferredpartyrevised[i,1:3]) == sort(preferredpartyrevised[i,1:3])[2] & sort(preferredpartyrevised[i,1:3])[2] != 9)
		{finalpartyprefrevised[i,2] <- 2/3}
		else{if(modefx(preferredpartyrevised[i,1:3]) == sort(preferredpartyrevised[i,1:3])[2] & sort(preferredpartyrevised[i,1:3])[2] == 9)
			{if(sort(preferredpartyrevised[i,1:3])[1] %in% c(1,2,3) & (modefx(preferredpartyrevised[i,4:10]) == sort(preferredpartyrevised[i,1:3])[1] | (preferredpartyrevised[i,4] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,5] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,6] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,7] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,8] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,9] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,10] %in% c(sort(preferredpartyrevised[i,1:3])[1],9))))
			{finalpartyprefrevised[i,2] <- hist(preferredpartyrevised[i,1:10], breaks = c(0:9))$counts[finalpartyprefrevised[i,1]]/10}
			else{if(sort(preferredpartyrevised[i,1:3])[1] %in% c(6,7) & (modefx(preferredpartyrevised[i,4:6]) == sort(preferredpartyrevised[i,1:3])[1] | (preferredpartyrevised[i,4] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,5] %in% c(sort(preferredpartyrevised[i,1:3])[1],9) & preferredpartyrevised[i,6] %in% c(sort(preferredpartyrevised[i,1:3])[1],9))))
				{finalpartyprefrevised[i,2] <- hist(preferredpartyrevised[i,1:6], breaks = c(0:9))$counts[finalpartyprefrevised[i,1]]/6}
				else{finalpartyprefrevised[i,2] <- 1/3}
					}
				}
		else{if(preferredpartyrevised[i,1] != preferredpartyrevised[i,2] & preferredpartyrevised[i,1] != preferredpartyrevised[i,3] & preferredpartyrevised[i,2] != preferredpartyrevised[i,3])
			{if((preferredpartyrevised[i,1] %in% c(6,7) | preferredpartyrevised[i,2] %in% c(6,7) | preferredpartyrevised[i,3] %in% c(6,7)) & (modefx9(preferredpartyrevised[i,4:6]) == preferredpartyrevised[i,1] | modefx9(preferredpartyrevised[i,4:6]) == preferredpartyrevised[i,2] | modefx9(preferredpartyrevised[i,4:6]) == preferredpartyrevised[i,3]))
			{finalpartyprefrevised[i,2] <- hist(preferredpartyrevised[i,1:6], breaks = c(0:9))$counts[finalpartyprefrevised[i,1]]/6}
			else{finalpartyprefrevised[i,2] <- hist(preferredpartyrevised[i,1:10], breaks = c(0:9))$counts[finalpartyprefrevised[i,1]]/10}	
			}
		}}}}}


#Create our histogram of party preferences 
hist(finalpartyprefrevised[finalpartyprefrevised[,1] != 9,2], breaks = c(seq(from = 0, to = 1, by = 1/10)), xlab = "Clarity Score Distribution", ylab = "Percent of Voters (n = 10466)", main = "Distribution of Clarity Scores, Among Voters \n for Whom Imputation Method Imputed a Preferred Party", col = "blue", axes = FALSE, ylim = c(0, 5500))

axis(1,c(seq(from = 0, to = 1, by = 1/10)), labels = c("0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8","0.9","1"))

axis(2,c(0,0.10*10466,0.20*10466,0.30*10466,0.40*10466,0.50*10466), labels = c("0%", "10%","20%","30%","40%","50%"))



#EPI estimation of party preference
postprefptyrevised <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 9)

postprefptyrevised <- cbind(postprefpty[,2], postprefpty[,4], postprefpty[,1], postprefpty[,10], postprefpty[,3], postprefpty[,6], postprefpty[,7], postprefpty[,8], postprefpty[,9])

postfinalpartyprefrevised <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 2)


for(i in 1:nrow(preferredpartyrevised)){
	if(postprefptyrevised[i,1] %in% c(4,5,8) | postprefptyrevised[i,3] %in% c(4,5,8))
	{if(postprefptyrevised[i,1] == postprefptyrevised[i,3] | (postprefptyrevised[i,1] %in% 9 | postprefptyrevised[i,3] %in% 9))
		{postfinalpartyprefrevised[i,1] <- modefx9(postprefptyrevised[i,c(1,3)])}
		else{postfinalpartyprefrevised[i,1] <- 9}
		}
	else{if(postprefptyrevised[i,1] == postprefptyrevised[i,2] & postprefptyrevised[i,1] == postprefptyrevised[i,3] & postprefptyrevised[i,2] == postprefptyrevised[i,3])
		{postfinalpartyprefrevised[i,1] <- postprefptyrevised[i,2]}
		else{if(modefx(postprefptyrevised[i,1:3]) == sort(postprefptyrevised[i,1:3])[2] & sort(postprefptyrevised[i,1:3])[2] != 9)
			{postfinalpartyprefrevised[i,1] <- modefx(postprefptyrevised[i,1:3])}
			else{if(modefx(postprefptyrevised[i,1:3]) == sort(postprefptyrevised[i,1:3])[2] & sort(postprefptyrevised[i,1:3])[2] == 9)
				{if(sort(postprefptyrevised[i,1:3])[1] %in% c(1,2,3) & (modefx(postprefptyrevised[i,4:9]) == sort(postprefptyrevised[i,1:3])[1] | (postprefptyrevised[i,4] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,5] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,6] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,7] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,8] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,9] %in% c(sort(postprefptyrevised[i,1:3])[1],9))))
					{postfinalpartyprefrevised[i,1] <- sort(postprefptyrevised[i,1:3])[1]}
					else{if(sort(postprefptyrevised[i,1:3])[1] %in% c(6,7) & (modefx(postprefptyrevised[i,4:5]) == sort(postprefptyrevised[i,1:3])[1] | (postprefptyrevised[i,4] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,5] %in% c(sort(postprefptyrevised[i,1:3])[1],9))))
						{postfinalpartyprefrevised[i,1] <- sort(postprefptyrevised[i,1:3])[1]}
						else{postfinalpartyprefrevised[i,1] <- 9}
						}
					}
				else{if(postprefptyrevised[i,1] != postprefptyrevised[i,2] & postprefptyrevised[i,1] != postprefptyrevised[i,3] & postprefptyrevised[i,2] != postprefptyrevised[i,3])
					{if((postprefptyrevised[i,1] %in% c(6,7) | postprefptyrevised[i,2] %in% c(6,7) | postprefptyrevised[i,3] %in% c(6,7)) & (postprefptyrevised[i,4] %in% c(postprefptyrevised[i,1],postprefptyrevised[i,2],postprefptyrevised[i,3]) & postprefptyrevised[i,5] %in% c(postprefptyrevised[i,4],9)))
						{postfinalpartyprefrevised[i,1] <- postprefptyrevised[i,4]}
						else{if((postprefptyrevised[i,1] %in% c(6,7) | postprefptyrevised[i,2] %in% c(6,7) | postprefptyrevised[i,3] %in% c(6,7)) & (postprefptyrevised[i,5] %in% c(postprefptyrevised[i,1],postprefptyrevised[i,2],postprefptyrevised[i,3]) & postprefptyrevised[i,4] %in% c(postprefptyrevised[i,5],9)))
							{postfinalpartyprefrevised[i,1] <- postprefptyrevised[i,5]}
							else{if(modefx9(postprefptyrevised[i,4:9]) == postprefptyrevised[i,1] | modefx9(postprefptyrevised[i,4:9]) == postprefptyrevised[i,2] | modefx9(postprefptyrevised[i,4:9]) == postprefptyrevised[i,3])
								{postfinalpartyprefrevised[i,1] <- modefx9(postprefptyrevised[i,4:9])}
								else{postfinalpartyprefrevised[i,1] <- 9}
								}
							}
						}
					else{postfinalpartyprefrevised[i,1] <- 9}
					}
				}
			}
		}
	}
		

#now let's histogram this stuff; first the imputed preferences
hist(postfinalpartyprefrevised[,1], breaks = c(0,1,2,3,4,5,6,7,8,9), xlab = "Party Preference, Post-Election Imputation", ylab = "Percent of Voters", col = "red", main = "Party Preferences, Post-Election Imputation", axes = FALSE, ylim = c(0,0.36*nrow(pevotedata)))

axis(1,c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5), labels = c("Con", "Lab", "LDem", "BNP", "UKIP", "SNP", "PC", "Green", "DNK"))

axis(2,c(0,0.06*nrow(pevotedata),0.12*nrow(pevotedata),0.18*nrow(pevotedata),0.24*nrow(pevotedata),0.30*nrow(pevotedata), 0.36*nrow(pevotedata)), labels = c("0%", "6%","12%","18%","24%","30%","36\%"))

#now, let's histogram the metric 1 preferences
hist(postprefptyrevised[,1], breaks = c(0,1,2,3,4,5,6,7,8,9), xlab = "Party Preference, Post-Election Imputation", ylab = "Percent of Voters", col = "red", main = "Party Preferences, Post-Election Metric 1", axes = FALSE, ylim = c(0,0.36*nrow(pevotedata)))

axis(1,c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5), labels = c("Con", "Lab", "LDem", "BNP", "UKIP", "SNP", "PC", "Green", "DNK"))

axis(2,c(0,0.06*nrow(pevotedata),0.12*nrow(pevotedata),0.18*nrow(pevotedata),0.24*nrow(pevotedata),0.30*nrow(pevotedata), 0.36*nrow(pevotedata)), labels = c("0%", "6%","12%","18%","24%","30%","36\%"))

#now for the clarity scores
for(i in 1:nrow(preferredpartyrevised)){
	if(postfinalpartyprefrevised[i,1] == 9)
	{postfinalpartyprefrevised[i,2] <- 0}
	else{if((postprefptyrevised[i,1] %in% c(4,5,8) | postprefptyrevised[i,3] %in% c(4,5,8)) & (postprefptyrevised[i,1] %in% 9 | postprefptyrevised[i,3] %in% 9))
		{postfinalpartyprefrevised[i,2] <- 1/2}
		else{if((postprefptyrevised[i,1] %in% c(4,5,8) | postprefptyrevised[i,3] %in% c(4,5,8)) & (postprefptyrevised[i,1] == postprefptyrevised[i,3]))
			{postfinalpartyprefrevised[i,2] <- 1}
			else{if(postprefptyrevised[i,1] == postprefptyrevised[i,2] & postprefptyrevised[i,1] == postprefptyrevised[i,3] & postprefptyrevised[i,2] == postprefptyrevised[i,3])
			{postfinalpartyprefrevised[i,2] <- 1}			else{if(modefx(postprefptyrevised[i,1:3]) == sort(postprefptyrevised[i,1:3])[2] & sort(postprefptyrevised[i,1:3])[2] != 9)
				{postfinalpartyprefrevised[i,2] <- 2/3}
				else{if(modefx(postprefptyrevised[i,1:3]) == sort(postprefptyrevised[i,1:3])[2] & sort(postprefptyrevised[i,1:3])[2] == 9)
					{if(sort(postprefptyrevised[i,1:3])[1] %in% c(1,2,3) & (modefx(postprefptyrevised[i,4:9]) == sort(postprefptyrevised[i,1:3])[1] | (postprefptyrevised[i,4] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,5] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,6] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,7] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,8] %in% c(sort(postprefptyrevised[i,1:3])[1],9) & postprefptyrevised[i,9] %in% c(sort(postprefptyrevised[i,1:3])[1],9))))
						{postfinalpartyprefrevised[i,2] <- hist(postprefptyrevised[i,1:9], breaks = c(0:9))$counts[postfinalpartyprefrevised[i,1]]/9}
						else{postfinalpartyprefrevised[i,2] <- hist(postprefptyrevised[i,1:5], breaks = c(0:9))$counts[postfinalpartyprefrevised[i,1]]/5}
					}
					else{if(postprefptyrevised[i,1] != postprefptyrevised[i,2] & postprefptyrevised[i,1] != postprefptyrevised[i,3] & postprefptyrevised[i,2] != postprefptyrevised[i,3])
						{if((postprefptyrevised[i,1] %in% c(6,7) | postprefptyrevised[i,2] %in% c(6,7) | postprefptyrevised[i,3] %in% c(6,7)) & (postprefptyrevised[i,4] %in% c(postprefptyrevised[i,1],postprefptyrevised[i,2],postprefptyrevised[i,3]) & postprefptyrevised[i,5] %in% c(postprefptyrevised[i,4],9)))
							{postfinalpartyprefrevised[i,2] <- hist(postprefptyrevised[i,1:5], breaks = c(0:9))$counts[postfinalpartyprefrevised[i,1]]/5}
							else{if((postprefptyrevised[i,1] %in% c(6,7) | postprefptyrevised[i,2] %in% c(6,7) | postprefptyrevised[i,3] %in% c(6,7)) & (postprefptyrevised[i,5] %in% c(postprefptyrevised[i,1],postprefptyrevised[i,2],postprefptyrevised[i,3]) & postprefptyrevised[i,4] %in% c(postprefptyrevised[i,5],9)))
								{postfinalpartyprefrevised[i,2] <- hist(postprefptyrevised[i,1:5], breaks = c(0:9))$counts[postfinalpartyprefrevised[i,1]]/5}
								else{postfinalpartyprefrevised[i,2] <- hist(postprefptyrevised[i,1:9], breaks = c(0:9))$counts[postfinalpartyprefrevised[i,1]]/9}
									}
								}
						else{postfinalpartyprefrevised[i,2] <- 0}
						
						
						}
					}	
				}
			}
		}
	}	
}


#now let's graph the clarity score distribution
hist(postfinalpartyprefrevised[postfinalpartyprefrevised[,1] != 9,2], breaks = c(seq(from = 0, to = 1, by = 1/10)), xlab = "Clarity Score Distribution", ylab = "Percent of Voters (n = 10816)", main = "Distribution of Post-Election Clarity Scores, Among Voters \n for Whom Imputation Method Imputed a Preferred Party", col = "blue", axes = FALSE, ylim = c(0, 6500))

axis(1,c(seq(from = 0, to = 1, by = 1/10)), labels = c("0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8","0.9","1"))

axis(2,c(0,0.10*10816,0.20*10816,0.30*10816,0.40*10816,0.50*10816, 0.60*10816), labels = c("0%", "10%","20%","30%","40%","50%","60%"))










pevotedata$stacticalcondensed <- as.numeric(recode(pevotedata$ccq39, "'i voted tactically' = 1; 'i really preferred another party but it stood no chance of w' = 1; else = 0")) - 1

pevotedata$stactical3 <- as.numeric(recode(pevotedata$ccq39, "'i voted tactically' = 0; 'i really preferred another party but it stood no chance of w' = 1; else = 0")) - 1

pevotedata$stactical4 <- as.numeric(recode(pevotedata$ccq39, "'i voted tactically' = 1; 'i really preferred another party but it stood no chance of w' = 0; else = 0")) - 1



pevotedata$aaq43condensed <- as.numeric(recode(pevotedata$aaq43, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq44condensed <- as.numeric(recode(pevotedata$aaq44, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq45condensed <- as.numeric(recode(pevotedata$aaq45, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq46condensed <- as.numeric(recode(pevotedata$aaq46, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq47condensed <- as.numeric(recode(pevotedata$aaq47, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq48condensed <- as.numeric(recode(pevotedata$aaq48, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

voteoutcome <- cbind(pevotedata$aaq43condensed,pevotedata$aaq44condensed,pevotedata$aaq45condensed,pevotedata$aaq46condensed,pevotedata$aaq47condensed,pevotedata$aaq48condensed)
voteoutcome <- as.matrix(voteoutcome)


#EAP
#examining our first tactical voting criteria
pevotechoice <- votechoice[votechoice != 10]
tacticalmatrixrevised <- matrix(data = NA, nrow = length(pevotechoice), ncol = 4)
for(i in 1:length(pevotechoice)){
	if(finalpartyprefrevised[i,1] == pevotechoice[i])
	{tacticalmatrixrevised[i,1] <- 0}
	else{if(finalpartyprefrevised[i,1] %in% 9)
		{tacticalmatrixrevised[i,1] <- 0}
		else{tacticalmatrixrevised[i,1] <- 1}}
		}
		
pevotedata$aaq43condensed <- as.numeric(recode(pevotedata$aaq43, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq44condensed <- as.numeric(recode(pevotedata$aaq44, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq45condensed <- as.numeric(recode(pevotedata$aaq45, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq46condensed <- as.numeric(recode(pevotedata$aaq46, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq47condensed <- as.numeric(recode(pevotedata$aaq47, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$aaq48condensed <- as.numeric(recode(pevotedata$aaq48, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

voteoutcome <- cbind(pevotedata$aaq43condensed,pevotedata$aaq44condensed,pevotedata$aaq45condensed,pevotedata$aaq46condensed,pevotedata$aaq47condensed,pevotedata$aaq48condensed)
voteoutcome <- as.matrix(voteoutcome)


voteoutcome2 <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 6)

for(i in 1:nrow(pevotedata)){
	for(j in 1:6){	
	if(voteoutcome[i,j] < sort(voteoutcome[i,])[5] & voteoutcome[i,j] != 1)
	{voteoutcome2[i,j] <- 1}
	else{voteoutcome2[i,j] <- 0}
	}}

#now the second criterion
for(i in 1:nrow(pevotedata)){
	if(finalpartyprefrevised[i,1] %in% 1)
	{tacticalmatrixrevised[i,2] <- voteoutcome2[i,2]}
	else{if(finalpartyprefrevised[i,1] %in% 2)
		{tacticalmatrixrevised[i,2] <- voteoutcome2[i,1]}
		else{if(finalpartyprefrevised[i,1] %in% 3)
			{tacticalmatrixrevised[i,2] <- voteoutcome2[i,3]}
			else{if(finalpartyprefrevised[i,1] %in% 6)
				{tacticalmatrixrevised[i,2] <- voteoutcome2[i,4]}
				else{if(finalpartyprefrevised[i,1] %in% 7)
					{tacticalmatrixrevised[i,2] <- voteoutcome2[i,5]}
					else{tacticalmatrixrevised[i,2] <- 0}
						}
					}
				}
			}
		}


#now alternate strategy 1: assuming that the responses to the last question refer to most preferred party
tacticalmatrixrevisedalt1 <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 4)
tacticalmatrixrevisedalt1[,1] <- tacticalmatrixrevised[,1] 

for(i in 1:nrow(pevotedata)){
	if(finalpartyprefrevised[i,1] %in% 1)
	{tacticalmatrixrevisedalt1[i,2] <- voteoutcome2[i,2]}
	else{if(finalpartyprefrevised[i,1] %in% 2)
		{tacticalmatrixrevisedalt1[i,2] <- voteoutcome2[i,1]}
		else{if(finalpartyprefrevised[i,1] %in% 3)
			{tacticalmatrixrevisedalt1[i,2] <- voteoutcome2[i,3]}
			else{if(finalpartyprefrevised[i,1] %in% 6)
				{tacticalmatrixrevisedalt1[i,2] <- voteoutcome2[i,4]}
				else{if(finalpartyprefrevised[i,1] %in% 7)
					{tacticalmatrixrevisedalt1[i,2] <- voteoutcome2[i,5]}
					else{if(finalpartyprefrevised[i,1] %in% c(4,5,8))
						{tacticalmatrixrevisedalt1[i,2] <- voteoutcome2[i,6]}
						else{tacticalmatrixrevisedalt1[i,2] <- 0}
						}
					}
				}
			}
		}
	}
	
	
#testing the alternative criterion
sum(voteoutcome2[finalpartyprefrevised[,1] %in% c(4,5,8),6])
length(voteoutcome2[finalpartyprefrevised[,1] %in% c(4,5,8),6])
length(voteoutcome[finalpartyprefrevised[,1] %in% c(4,5,8) & voteoutcome2[,6] == 0 & voteoutcome[,6] %in% c(3:12),6])



#now let's make the alternative real!
tacticalmatrixrevised[,2] <- tacticalmatrixrevisedalt1[,2]	
	
#now the third criterion
voteoutcomealt <- cbind(voteoutcome[,2], voteoutcome[,1], voteoutcome[,3], voteoutcome[,6], voteoutcome[,6], voteoutcome[,4], voteoutcome[,5], voteoutcome[,6], 1)


for(i in 1:nrow(pevotedata)){
	if(tacticalmatrixrevised[i,1] == 0)
	{tacticalmatrixrevised[i,3] <- 0}
	else{if(voteoutcomealt[i,pevotechoice[i]] > voteoutcomealt[i,finalpartyprefrevised[i,1]] & voteoutcomealt[i,pevotechoice[i]] != 1 & voteoutcomealt[i,finalpartyprefrevised[i,1]] != 1)
	{tacticalmatrixrevised[i,3] <- 1}
	else{tacticalmatrixrevised[i,3] <- 0}
	}}

abc <- matrix(data = NA, nrow = nrow(tacticalmatrixrevised), ncol = 1)
for(i in 1:nrow(pevotedata)){
	if(voteoutcomealt[i,pevotechoice[i]] == 1 | voteoutcomealt[i,finalpartyprefrevised[i,1]] != 1)
	{abc[i,1] <- 1}
	else{abc[i,1] <- 0}}
	
	
#now who voted tactically?
for(i in 1:nrow(pevotedata)){
	if(tacticalmatrixrevised[i,1] == 1 & tacticalmatrixrevised[i,2] == 1 & tacticalmatrixrevised[i,3] == 1)
	{tacticalmatrixrevised[i,4] <- 1}
	else{tacticalmatrixrevised[i,4] <- 0}}
	
#Let's graph their initial preferred party
hist(finalpartyprefrevised[tacticalmatrixrevised[,4] == 1,1], breaks = c(0:9))$counts


#and the self-identification, etc.
sum(tacticalmatrixrevised[pevotedata$stactical3 == 1,4])

sum(tacticalmatrixrevised[pevotedata$stactical4 == 1,4])



#EPI

#now let's do the first criterion
posttacticalmatrixrevised <- matrix(data = NA, nrow = length(pevotechoice), ncol = 4)

for(i in 1:length(pevotechoice)){
	if(postfinalpartyprefrevised[i,1] == pevotechoice[i])
	{posttacticalmatrixrevised[i,1] <- 0}
	else{if(postfinalpartyprefrevised[i,1] %in% 9)
		{posttacticalmatrixrevised[i,1] <- 0}
		else{posttacticalmatrixrevised[i,1] <- 1}}
		}






#OK, now what's next - the second and third criteria; start with BESdataanalysis122810.R
ptyperform <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 8)
ptyperform <- cbind(pevotedata$con10, pevotedata$lab10, pevotedata$ld10, pevotedata$bnp10, pevotedata$ukip10, pevotedata$snp10, pevotedata$pc10, pevotedata$grn10)
ptyperform <- as.matrix(ptyperform)

ptyperform[10691:10696,] <- ptyperform[10697:10702,]
ptyperform[1:2,] <- ptyperform[3:4,]

for(i in 1:nrow(ptyperform)){
	for(j in 1:8){
		if(is.na(ptyperform[i,j]) == TRUE)
		{ptyperform[i,j] <- 0}
		else{ptyperform[i,j] <- ptyperform[i,j]}
		}}

thirdworseactual <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 8)

for(i in 1:nrow(pevotedata)){
	for(j in 1:8){	
	if(ptyperform[i,j] < sort(ptyperform[i,])[7])
	{thirdworseactual[i,j] <- 1}
	else{thirdworseactual[i,j] <- 0}
	}}

for(i in 1:nrow(pevotedata)){
	if(postfinalpartyprefrevised[i,1] %in% 1)
	{posttacticalmatrixrevised[i,2] <- thirdworseactual[i,1]}
	else{if(postfinalpartyprefrevised[i,1] %in% 2)
		{posttacticalmatrixrevised[i,2] <- thirdworseactual[i,2]}
		else{if(postfinalpartyprefrevised[i,1] %in% 3)
			{posttacticalmatrixrevised[i,2] <- thirdworseactual[i,3]}
			else{if(postfinalpartyprefrevised[i,1] %in% 6)
				{posttacticalmatrixrevised[i,2] <- thirdworseactual[i,6]}
				else{if(postfinalpartyprefrevised[i,1] %in% 7)
					{posttacticalmatrixrevised[i,2] <- thirdworseactual[i,7]}
					else{if(postfinalpartyprefrevised[i,1] %in% 4)
						{posttacticalmatrixrevised[i,2] <- thirdworseactual[i,4]}
						else{if(postfinalpartyprefrevised[i,1] %in% 5)
						{posttacticalmatrixrevised[i,2] <- thirdworseactual[i,5]}
							else{if(postfinalpartyprefrevised[i,1] %in% 8)
								{posttacticalmatrixrevised[i,2] <- thirdworseactual[i,8]}
							
							else{posttacticalmatrixrevised[i,2] <- 0}
						}
					}
				}
			}
		}
	}}}

#how many of those who thought their preferred party would finish in third or worse actually ended up having this actually happen?
sum(posttacticalmatrixrevised[tacticalmatrixrevised[,2] == 1,2])




#now the third criterion
for(i in 1:nrow(pevotedata)){
	if(postfinalpartyprefrevised[i,1] %in% c(1:8) & pevotechoice[i] %in% c(1:8))
	{if(ptyperform[i,postfinalpartyprefrevised[i,1]] < ptyperform[i,pevotechoice[i]])
		{posttacticalmatrixrevised[i,3] <- 1}
		else{posttacticalmatrixrevised[i,3] <- 0}
		}
		else{posttacticalmatrixrevised[i,3] <- 0}
		}

#finally, let's put it together
for(i in 1:nrow(pevotedata)){
	if(posttacticalmatrixrevised[i,1] == 1 & posttacticalmatrixrevised[i,2] == 1 & posttacticalmatrixrevised[i,3] == 1)
	{posttacticalmatrixrevised[i,4] <- 1}
	else{posttacticalmatrixrevised[i,4] <- 0}
	}
	
#let's analyze our set of tactical voters
sum(posttacticalmatrixrevised[tacticalmatrixrevised[,4] == 1 & posttacticalmatrixrevised[,4] == 1,4])

length(posttacticalmatrixrevised[posttacticalmatrixrevised[,4] == 1 & pevotedata$stactical3 ==1,4])

length(posttacticalmatrixrevised[posttacticalmatrixrevised[,4] == 1 & pevotedata$stactical4 ==1,4])

length(posttacticalmatrixrevised[posttacticalmatrixrevised[,4] == 0 & tacticalmatrixrevised[,4] == 0 & pevotedata$stacticalcondensed == 0,4])




#PPCE
#let's do the campaign expectations stuff
tacticalmatrixrevised2 <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 4)

tacticalmatrixrevised2[,1] <- posttacticalmatrixrevised[,1]
 
pevotedata$bbq33condensed <- as.numeric(recode(pevotedata$bbq33, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$bbq34condensed <- as.numeric(recode(pevotedata$bbq34, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$bbq35condensed <- as.numeric(recode(pevotedata$bbq35, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$bbq36condensed <- as.numeric(recode(pevotedata$bbq36, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

pevotedata$bbq37condensed <- as.numeric(recode(pevotedata$bbq37, "'0-very unlikely' = -2; '1' = 0; '2' = 1; '3' = 2; '4' = 3; '5' = 4; '6' = 5; '7' = 6; '8' = 7; '9' = 8; '10-very likely' = 9; else = -1"))

voteoutcomecamp <- cbind(pevotedata$bbq33condensed, pevotedata$bbq34condensed, pevotedata$bbq35condensed, pevotedata$bbq36condensed, pevotedata$bbq37condensed)

voteoutcomecamp <- as.matrix(voteoutcomecamp)

voteoutcomecamp2 <- matrix(data = NA, nrow = nrow(pevotedata), ncol = 5)

for(i in 1:nrow(pevotedata)){
	for(j in 1:5){
		if(voteoutcomecamp[i,j] < sort(voteoutcomecamp[i,])[4] & voteoutcomecamp[i,j] != 1)
		{voteoutcomecamp2[i,j] <- 1}
		else{voteoutcomecamp2[i,j] <- 0}
	}}


#creating the second criterion
for(i in 1:nrow(pevotedata)){
	if(postfinalpartyprefrevised[i,1] %in% 1)
	{tacticalmatrixrevised2[i,2] <- voteoutcomecamp2[i,2]}
	else{if(postfinalpartyprefrevised[i,1] %in% 2)
		{tacticalmatrixrevised2[i,2] <- voteoutcomecamp2[i,1]}
		else{if(postfinalpartyprefrevised[i,1] %in% 3)
			{tacticalmatrixrevised2[i,2] <- voteoutcomecamp2[i,3]}
			else{if(postfinalpartyprefrevised[i,1] %in% 6)
				{tacticalmatrixrevised2[i,2] <- voteoutcomecamp2[i,4]}
				else{if(postfinalpartyprefrevised[i,1] %in% 7)
					{tacticalmatrixrevised2[i,2] <- voteoutcomecamp2[i,5]}
					else{tacticalmatrixrevised2[i,2] <- 0}
						}
					}
				}
			}
		}

sum(tacticalmatrixrevised[finalpartyprefrevised[,1] %in% c(1,2,3,6,7,9),2])
length(tacticalmatrixrevised[finalpartyprefrevised[,1] %in% c(1,2,3,6,7,9),2])

sum(tacticalmatrixrevised2[postfinalpartyprefrevised[,1] %in% c(1,2,3,6,7,9),2])
length(tacticalmatrixrevised2[postfinalpartyprefrevised[,1] %in% c(1,2,3,6,7,9),2])

sum(tacticalmatrixrevised2[postfinalpartyprefrevised[,1] %in% c(1,2,3,6,7,9) & finalpartyprefrevised[,1] %in% c(1,2,3,6,7,9) & tacticalmatrixrevised[,2] == 1,2])

length(tacticalmatrixrevised2[postfinalpartyprefrevised[,1] %in% c(1,2,3,6,7,9) & finalpartyprefrevised[,1] %in% c(1,2,3,6,7,9) & postfinalpartyprefrevised[,1] == finalpartyprefrevised[,1],2])

sum(tacticalmatrixrevised2[postfinalpartyprefrevised[,1] %in% c(1,2,3,6,7,9) & finalpartyprefrevised[,1] %in% c(1,2,3,6,7,9) & postfinalpartyprefrevised[,1] == finalpartyprefrevised[,1] & tacticalmatrixrevised[,2] == 1,2])

#third criterion
voteoutcomecampalt <- cbind(voteoutcomecamp[,2],voteoutcomecamp[,1],voteoutcomecamp[,3],1,1,voteoutcomecamp[,4],voteoutcomecamp[,5],1,1)

for(i in 1:nrow(pevotedata)){
	if(tacticalmatrixrevised2[i,1] == 0)
	{tacticalmatrixrevised2[i,3] <- 0}
	else{if(voteoutcomecampalt[i,pevotechoice[i]] > voteoutcomecampalt[i,postfinalpartyprefrevised[i,1]] & voteoutcomecampalt[i,pevotechoice[i]] != 1 & voteoutcomealt[i,postfinalpartyprefrevised[i,1]] != 1)
	{tacticalmatrixrevised2[i,3] <- 1}
	else{tacticalmatrixrevised2[i,3] <- 0}
	}}
	
length(tacticalmatrixrevised2[tacticalmatrixrevised[,3] == 1,3])

sum(tacticalmatrixrevised2[tacticalmatrixrevised])


#let's put it together
for(i in 1:nrow(pevotedata)){
	if(tacticalmatrixrevised2[i,1] == 1 & tacticalmatrixrevised2[i,2] == 1 & tacticalmatrixrevised2[i,3] == 1)
	{tacticalmatrixrevised2[i,4] <- 1}
	else{tacticalmatrixrevised2[i,4] <- 0}
	}

votertableboot <- matrix(data = NA, nrow = 12180, ncol = 24)

votertableboot[,1] <- tacticalmatrixrevised[,1]
votertableboot[,2] <- tacticalmatrixrevised[,2]
votertableboot[,3] <- tacticalmatrixrevised[,3]
votertableboot[,4] <- tacticalmatrixrevised[,4]
votertableboot[,5] <- posttacticalmatrixrevised[,1]
votertableboot[,6] <- posttacticalmatrixrevised[,2]
votertableboot[,7] <- posttacticalmatrixrevised[,3]
votertableboot[,8] <- posttacticalmatrixrevised[,4]
votertableboot[,9] <- posttacticalmatrixrevised[,1]
votertableboot[,10] <- tacticalmatrixrevised2[,2]
votertableboot[,11] <- tacticalmatrixrevised2[,3]
votertableboot[,12] <- tacticalmatrixrevised2[,4]
votertableboot[,13] <- pevotedata$stacticalcondensed
votertableboot[,14] <- pevotedata$stactical3
votertableboot[,15] <- pevotedata$stactical4

for(i in 1:nrow(votertableboot)){
	if(votertableboot[i,4] == 1 | votertableboot[i,8] == 1 | votertableboot[i,12] == 1)
	{votertableboot[i,16] <- 0}
	else{votertableboot[i,16] <- 1}}

voteoutcomenew <- cbind(voteoutcome[,2], voteoutcome[,1], voteoutcome[,3], voteoutcome[,6], voteoutcome[,6], voteoutcome[,4], voteoutcome[,5], voteoutcome[,6],1)

for(i in 1:nrow(votertableboot)){
	if(finalpartyprefrevised[i,1] == 9 | pevotechoice[i] == 9)
	{votertableboot[i,17] <- 0}
	else{if(voteoutcomenew[i,finalpartyprefrevised[i,1]] == 1)
		{votertableboot[i,17] <- 0}
		else{if(voteoutcomenew[i,pevotechoice[i]] == 1)
			{votertableboot[i,17] <- 0}
			else{votertableboot[i,17] <- 1}
		}}}

#let's test the metric
sum(votertableboot[votertableboot[,13] == 1 & votertableboot[,16] == 1,17])
	

for(i in 1:nrow(votertableboot)){
	if(postfinalpartyprefrevised[i,1] == 9)
	{votertableboot[i,18] <- 0}
	else{votertableboot[i,18] <- 1}}



voteoutcomecampnew <- cbind(voteoutcomecamp[,2], voteoutcomecamp[,1], voteoutcomecamp[,3], 1, 1, voteoutcomecamp[,4], voteoutcomecamp[,5], 1,1)


for(i in 1:nrow(votertableboot)){
	if(postfinalpartyprefrevised[i,1] %in% c(4,5,8,9))
	{votertableboot[i,19] <- 0}
	else{if(voteoutcomecampnew[i,postfinalpartyprefrevised[i,1]] == 1)
		{votertableboot[i,19] <- 0}
		else{if(voteoutcomecampnew[i, pevotechoice[i]] == 1)
			{votertableboot[i,19] <- 0}
			else{votertableboot[i,19] <- 1}
		}	
	}}
	
#let's test the metric
sum(votertableboot[votertableboot[,13] == 1 & votertableboot[,16] == 1,19])

for(i in 1:nrow(votertableboot)){
	if(votertableboot[i,17] == 1 | votertableboot[i,18] == 1 | votertableboot[i,19] == 1)
	{votertableboot[i,20] <- 1}
	else{votertableboot[i,20] <- 0}
	}

for(i in 1:nrow(votertableboot)){
	if(voteoutcomenew[i,finalpartyprefrevised[i,1]] == sort(voteoutcomenew[i,])[9] & voteoutcomenew[i,finalpartyprefrevised[i,1]] != 1)
	{votertableboot[i,21] <- 1}
	else{votertableboot[i,21] <- 0}
}

length(votertableboot[votertableboot[,16] == 1 & votertableboot[,19] == 1 & votertableboot[,14] == 1,21])

for(i in 1:nrow(votertableboot)){
	if(voteoutcomecampnew[i,postfinalpartyprefrevised[i,1]] == sort(voteoutcomecampnew[i,])[9] & voteoutcomecampnew[i,postfinalpartyprefrevised[i,1]] != 1)
	{votertableboot[i,22] <- 1}
	else{votertableboot[i,22] <- 0}
}

votertableboot[,23] <- finalpartyprefrevised[,2]
votertableboot[,24] <- postfinalpartyprefrevised[,2]

#now that we've got our matrix, let's construct our method of selecting the 1000 voters
simvalues <- matrix(data = NA, nrow = 3000, ncol = 10000)
for(i in 1:ncol(simvalues)){
	simvalues[,i] <- sample(1:12180, 3000, replace = F)}

predictedvaluesmatrix <- matrix(data = NA, nrow = 10000, ncol = 9)

bootmatrixvoters <- matrix(data= NA, nrow = 3000, ncol = 24)


#for the first inquiry
for(i in 1:nrow(predictedvaluesmatrix)){
	bootmatrixvoters <- votertableboot[simvalues[,i],]
	
	predictedvaluesmatrix[i,1] <- sum(bootmatrixvoters[bootmatrixvoters[,13] == 1 & bootmatrixvoters[,20] == 1,16])/length(bootmatrixvoters[bootmatrixvoters[,13] == 1 & bootmatrixvoters[,20] == 1,16])
	
	predictedvaluesmatrix[i,2] <- sum(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,20] == 1,16])/length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,20] == 1,16])
	
	predictedvaluesmatrix[i,3] <- sum(bootmatrixvoters[bootmatrixvoters[,15] == 1 & bootmatrixvoters[,20] == 1,16])/length(bootmatrixvoters[bootmatrixvoters[,15] == 1 & bootmatrixvoters[,20] == 1,16])
	
	predictedvaluesmatrix[i,4] <- length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,17] == 1 & bootmatrixvoters[,21] == 1,21])/length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,17] == 1,21])
	
	predictedvaluesmatrix[i,5] <- length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,19] == 1 & bootmatrixvoters[,21] == 1,21])/length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,19] == 1,21])
		
	predictedvaluesmatrix[i,6] <-length(bootmatrixvoters[bootmatrixvoters[,1] == 0 & bootmatrixvoters[,5] == 0 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,13] == 1,1])/length(bootmatrixvoters[bootmatrixvoters[,20] == 1 & bootmatrixvoters[,13] == 1,1])
	
	predictedvaluesmatrix[i,7] <-length(bootmatrixvoters[bootmatrixvoters[,1] == 0 & bootmatrixvoters[,5] == 0 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,14] == 1,1])/length(bootmatrixvoters[bootmatrixvoters[,20] == 1 & bootmatrixvoters[,14] == 1,1])
	
	predictedvaluesmatrix[i,8] <-length(bootmatrixvoters[bootmatrixvoters[,1] == 0 & bootmatrixvoters[,5] == 0 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,15] == 1,1])/length(bootmatrixvoters[bootmatrixvoters[,20] == 1 & bootmatrixvoters[,15] == 1,1])
	
	predictedvaluesmatrix[i,9] <-length(bootmatrixvoters[bootmatrixvoters[,2] == 0 & bootmatrixvoters[,10] == 0 & (bootmatrixvoters[,17] == 1 | bootmatrixvoters[,19] == 1) & bootmatrixvoters[,14] == 1,1])/length(bootmatrixvoters[(bootmatrixvoters[,17] == 1 | bootmatrixvoters[,19] == 1) & bootmatrixvoters[,14] == 1,1])
	
	bootmatrixvoters <- matrix(data = NA, nrow = 1000, ncol = 24)
}



#Okay, now let's calculate the relevant estimates/CIs
mean(predictedvaluesmatrix[,1])
sort(predictedvaluesmatrix[,1])[250]
sort(predictedvaluesmatrix[,1])[9750]

mean(predictedvaluesmatrix[,2])
sort(predictedvaluesmatrix[,2])[250]
sort(predictedvaluesmatrix[,2])[9750]

mean(predictedvaluesmatrix[,3])
sort(predictedvaluesmatrix[,3])[250]
sort(predictedvaluesmatrix[,3])[9750]

mean(predictedvaluesmatrix[,4])
sort(predictedvaluesmatrix[,4])[250]
sort(predictedvaluesmatrix[,4])[9750]

mean(predictedvaluesmatrix[,5])
sort(predictedvaluesmatrix[,5])[250]
sort(predictedvaluesmatrix[,5])[9750]

mean(predictedvaluesmatrix[,6])
sort(predictedvaluesmatrix[,6])[250]
sort(predictedvaluesmatrix[,6])[9750]

mean(predictedvaluesmatrix[,7])
sort(predictedvaluesmatrix[,7])[250]
sort(predictedvaluesmatrix[,7])[9750]

mean(predictedvaluesmatrix[,8])
sort(predictedvaluesmatrix[,8])[250]
sort(predictedvaluesmatrix[,8])[9750]

mean(predictedvaluesmatrix[,9])
sort(predictedvaluesmatrix[,9])[250]
sort(predictedvaluesmatrix[,9])[9750]



#with the weights
for(i in 1:nrow(predictedvaluesmatrix)){
	bootmatrixvoters <- votertableboot[simvalues[,i],]
	
	predictedvaluesmatrix[i,1] <- (sum(bootmatrixvoters[bootmatrixvoters[,13] == 1 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,16] == 1 & bootmatrixvoters[,24] != 0,24])/sum(bootmatrixvoters[bootmatrixvoters[,13] == 1 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,24] != 0,24]))+(sum(bootmatrixvoters[bootmatrixvoters[,13] == 1 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,16] == 1 & bootmatrixvoters[,23] != 0,23])/sum(bootmatrixvoters[bootmatrixvoters[,13] == 1 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,23] != 0,23]))/2
	
	predictedvaluesmatrix[i,2] <- sum(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,20] == 1,16])/length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,20] == 1,16])
	
	predictedvaluesmatrix[i,3] <- sum(bootmatrixvoters[bootmatrixvoters[,15] == 1 & bootmatrixvoters[,20] == 1,16])/length(bootmatrixvoters[bootmatrixvoters[,15] == 1 & bootmatrixvoters[,20] == 1,16])
	
	predictedvaluesmatrix[i,4] <- length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,17] == 1 & bootmatrixvoters[,21] == 1,21])/length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,17] == 1,21])
	
	predictedvaluesmatrix[i,5] <- length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,19] == 1 & bootmatrixvoters[,21] == 1,21])/length(bootmatrixvoters[bootmatrixvoters[,14] == 1 & bootmatrixvoters[,19] == 1,21])
		
	predictedvaluesmatrix[i,6] <-length(bootmatrixvoters[bootmatrixvoters[,1] == 0 & bootmatrixvoters[,5] == 0 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,13] == 1,1])/length(bootmatrixvoters[bootmatrixvoters[,20] == 1 & bootmatrixvoters[,13] == 1,1])
	
	predictedvaluesmatrix[i,7] <-length(bootmatrixvoters[bootmatrixvoters[,1] == 0 & bootmatrixvoters[,5] == 0 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,14] == 1,1])/length(bootmatrixvoters[bootmatrixvoters[,20] == 1 & bootmatrixvoters[,14] == 1,1])
	
	predictedvaluesmatrix[i,8] <-length(bootmatrixvoters[bootmatrixvoters[,1] == 0 & bootmatrixvoters[,5] == 0 & bootmatrixvoters[,20] == 1 & bootmatrixvoters[,15] == 1,1])/length(bootmatrixvoters[bootmatrixvoters[,20] == 1 & bootmatrixvoters[,15] == 1,1])
	
	predictedvaluesmatrix[i,9] <-length(bootmatrixvoters[bootmatrixvoters[,2] == 0 & bootmatrixvoters[,10] == 0 & (bootmatrixvoters[,17] == 1 | bootmatrixvoters[,19] == 1) & bootmatrixvoters[,14] == 1,1])/length(bootmatrixvoters[(bootmatrixvoters[,17] == 1 | bootmatrixvoters[,19] == 1) & bootmatrixvoters[,14] == 1,1])
	
	bootmatrixvoters <- matrix(data = NA, nrow = 1000, ncol = 24)
}


#Article Appendix Code
#EAP
for(i in 1:nrow(finalpartyprefrevised)){
	if(preferredpartyrevised[i,1] == preferredpartyrevised[i,2])
	{finalpartyprefrevised[i,1] <- preferredpartyrevised[i,1]}
	else{finalpartyprefrevised[i,1] <- 9}
} 

#run the other metrics, from BESdataanalysis011911.R
nrow(tacticalmatrixrevised[tacticalmatrixrevised[,4] == 1 & pevotedata$stactical3 == 1,])
nrow(tacticalmatrixrevised[tacticalmatrixrevised[,4] == 1 & pevotedata$stactical4 == 1,])
nrow(tacticalmatrixrevised[tacticalmatrixrevised[,4] == 0 & pevotedata$stacticalcondensed == 1,])


#EPI
for(i in 1:nrow(postfinalpartyprefrevised)){
	if(preferredpartyrevised[i,1] == postprefptyrevised[i,2])
	{postfinalpartyprefrevised[i,1] <- postprefptyrevised[i,1]}
	else{postfinalpartyprefrevised[i,1] <- 9}
}

#run the other metrics, from BESdataanalysis012411.R
length(posttacticalmatrixrevised[posttacticalmatrixrevised[,4] == 1 & pevotedata$stactical3 ==1,4])
length(posttacticalmatrixrevised[posttacticalmatrixrevised[,4] == 1 & pevotedata$stactical4 ==1,4])


#PPCE
length(tacticalmatrixrevised2[tacticalmatrixrevised2[,4] == 1 & pevotedata$stactical3 == 1,4])
length(tacticalmatrixrevised2[tacticalmatrixrevised2[,4] == 1 & pevotedata$stactical4 == 1,4])

#now, let's run the bootstrappingprocedure.R code for the table
sum(votertableboot[votertableboot[,20] == 1,13])
sum(votertableboot[,13])

sum(votertableboot[votertableboot[,13] == 1 & votertableboot[,20] == 1,16])/length(votertableboot[votertableboot[,13] == 1 & votertableboot[,20] == 1,16])